Advertisement
Guest User

Untitled

a guest
May 17th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.12 KB | None | 0 0
  1. using BucketListApplication.Models.DB;
  2. using BucketListApplication.Models.EntityManager;
  3. using BucketListApplication.Models.ViewModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9.  
  10. namespace BucketListApplication.Controllers
  11. {
  12. public class ListController : Controller
  13. {
  14. #region ToDolIst View and Post for AddToList
  15. public ActionResult ToDoList()
  16. {
  17. return View();
  18. }
  19.  
  20.  
  21. // In the post I pass in the viewmodel that the view will use to collect information
  22. [HttpPost]
  23. public ActionResult ToDoList(BucketListView BLV)
  24. {
  25. if (ModelState.IsValid)
  26. {
  27. ListManager LM = new ListManager();
  28. // Addtolist is a method belonging to ListManager and can only be called if ListManager
  29. // object is created in the current context.
  30. LM.addToList(BLV);
  31. return RedirectToAction("MyToDoList", "Home");
  32.  
  33. }
  34. return View("Welcome", "Home");
  35. }
  36. #endregion
  37. }
  38. }
  39.  
  40. using System;
  41. using System.Collections.Generic;
  42. using System.ComponentModel.DataAnnotations;
  43. using System.Linq;
  44. using System.Web;
  45.  
  46. namespace BucketListApplication.Models.ViewModel
  47. {
  48. public class BucketListView
  49. {
  50. [Key]
  51. public int UserBucketListID { get; set; }
  52. public int SYSUserProfileID { get; set; }
  53. public Nullable<System.DateTime> BucketItemDate { get; set; }
  54. public string BucketItem { get; set; }
  55. public int BucketImportance { get; set; }
  56. }
  57. }
  58.  
  59. using System;
  60. using System.Collections.Generic;
  61. using System.Linq;
  62. using System.Web;
  63. using BucketListApplication.Models.DB;
  64. using BucketListApplication.Models.ViewModel;
  65.  
  66. namespace BucketListApplication.Models.EntityManager
  67. {
  68. public class ListManager
  69. {
  70. public void addToList(BucketListView BLV)
  71. {
  72. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  73. {
  74. UserBucketList UBL = new UserBucketList();
  75.  
  76. UBL.SYSUserProfileID = BLV.SYSUserProfileID;
  77. UBL.BucketItemDate = BLV.BucketItemDate;
  78. UBL.BucketItem = BLV.BucketItem;
  79. UBL.BucketImportance = BLV.BucketImportance;
  80. UBL.RowCreatedSYSUserID = BLV.SYSUserProfileID > 0 ? BLV.SYSUserProfileID : 1;
  81. UBL.RowModifiedSYSUserID = BLV.SYSUserProfileID > 0 ? BLV.SYSUserProfileID : 1; ;
  82. UBL.RowCreatedDateTime = DateTime.Now;
  83. UBL.RowModifiedDateTime = DateTime.Now;
  84.  
  85. db.UserBucketLists.Add(UBL);
  86. db.SaveChanges();
  87. }
  88. }
  89.  
  90. }
  91. }
  92.  
  93. @model IEnumerable<BucketListApplication.Models.DB.UserBucketList>
  94.  
  95. <p>
  96. @Html.ActionLink("Create New", "Create")
  97. </p>
  98. <table class="table">
  99. <tr>
  100. <th>
  101. @Html.DisplayNameFor(model => model.BucketItemDate)
  102. </th>
  103. <th>
  104. @Html.DisplayNameFor(model => model.BucketItem)
  105. </th>
  106. <th>
  107. @Html.DisplayNameFor(model => model.BucketImportance)
  108. </th>
  109. <th>
  110. @Html.DisplayNameFor(model => model.RowCreatedSYSUserID)
  111. </th>
  112. <th>
  113. @Html.DisplayNameFor(model => model.RowCreatedDateTime)
  114. </th>
  115. <th>
  116. @Html.DisplayNameFor(model => model.RowModifiedSYSUserID)
  117. </th>
  118. <th>
  119. @Html.DisplayNameFor(model => model.RowModifiedDateTime)
  120. </th>
  121. <th>
  122. @Html.DisplayNameFor(model => model.SYSUserProfile.FirstName)
  123. </th>
  124. <th></th>
  125. </tr>
  126.  
  127. @foreach (var item in Model) {
  128. <tr>
  129. <td>
  130. @Html.DisplayFor(modelItem => item.BucketItemDate)
  131. </td>
  132. <td>
  133. @Html.DisplayFor(modelItem => item.BucketItem)
  134. </td>
  135. <td>
  136. @Html.DisplayFor(modelItem => item.BucketImportance)
  137. </td>
  138. <td>
  139. @Html.DisplayFor(modelItem => item.RowCreatedSYSUserID)
  140. </td>
  141. <td>
  142. @Html.DisplayFor(modelItem => item.RowCreatedDateTime)
  143. </td>
  144. <td>
  145. @Html.DisplayFor(modelItem => item.RowModifiedSYSUserID)
  146. </td>
  147. <td>
  148. @Html.DisplayFor(modelItem => item.RowModifiedDateTime)
  149. </td>
  150. <td>
  151. @Html.DisplayFor(modelItem => item.SYSUserProfile.FirstName)
  152. </td>
  153. <td>
  154. @Html.ActionLink("Edit", "Edit", new { id=item.UserBucketListID }) |
  155. @Html.ActionLink("Details", "Details", new { id=item.UserBucketListID }) |
  156. @Html.ActionLink("Delete", "Delete", new { id=item.UserBucketListID })
  157. </td>
  158. </tr>
  159. }
  160.  
  161. </table>
  162. <div>
  163. <a href="@Url.Action("ToDoList", "List")" class="Button">Add More Items</a>
  164. </div>
  165.  
  166. using System;
  167. using System.Collections.Generic;
  168. using System.Linq;
  169. using System.Web;
  170. using System.Web.Mvc;
  171. using BucketListApplication.Models.ViewModel;
  172. using BucketListApplication.Models.EntityManager;
  173. using System.Web.Security;
  174.  
  175. namespace BucketListApplication.Controllers
  176. {
  177. public class AccountController : Controller
  178. {
  179. public ActionResult SignUp()
  180. {
  181. return View();
  182. }
  183.  
  184. [HttpPost]
  185. public ActionResult SignUp(UserSignUpView USV)
  186. {
  187. if (ModelState.IsValid)
  188. {
  189. UserManager UM = new UserManager();
  190. if (!UM.IsLoginNameExist(USV.LoginName))
  191. {
  192. UM.AddUserAccount(USV);
  193. FormsAuthentication.SetAuthCookie(USV.FirstName, false);
  194. return RedirectToAction("Welcome", "Home");
  195.  
  196. }
  197. else
  198. ModelState.AddModelError("", "Login Name already taken.");
  199. }
  200. return View();
  201. }
  202.  
  203.  
  204. public ActionResult LogIn()
  205. {
  206. return View();
  207. }
  208.  
  209. [HttpPost]
  210. public ActionResult LogIn(UserLoginView ULV, string returnUrl)
  211. {
  212. if (ModelState.IsValid)
  213. {
  214. UserManager UM = new UserManager();
  215. string password = UM.GetUserPassword(ULV.LoginName);
  216.  
  217. if (string.IsNullOrEmpty(password))
  218. ModelState.AddModelError("", "The user login or password provided is incorrect.");
  219. else
  220. {
  221. if (ULV.Password.Equals(password))
  222. {
  223. FormsAuthentication.SetAuthCookie(ULV.LoginName, false);
  224. return RedirectToAction("Welcome", "Home");
  225. }
  226. else
  227. {
  228. ModelState.AddModelError("", "The password provided is incorrect.");
  229. }
  230. }
  231. }
  232.  
  233. // If we got this far, something failed, redisplay form
  234. return View(ULV);
  235. }
  236.  
  237. [Authorize]
  238. public ActionResult SignOut()
  239. {
  240. FormsAuthentication.SignOut();
  241. return RedirectToAction("Index", "Home");
  242. }
  243. }
  244. }
  245.  
  246. using System;
  247. using System.Collections.Generic;
  248. using System.Linq;
  249. using System.Web;
  250. using System.Web.Mvc;
  251. using System.Web.Security;
  252. using BucketListApplication.Models.ViewModel;
  253. using BucketListApplication.Models.EntityManager;
  254. using BucketListApplication.Security;
  255. using BucketListApplication.Models.DB;
  256.  
  257. namespace BucketListApplication.Controllers
  258. {
  259. public class HomeController : Controller
  260. {
  261. public ActionResult Index()
  262. {
  263. return View();
  264. }
  265.  
  266. [Authorize]
  267. public ActionResult Welcome()
  268. {
  269. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  270. {
  271. return View(db.UserBucketLists.ToList());
  272. }
  273. }
  274.  
  275. [AuthorizeRoles("Admin")]
  276. public ActionResult AdminOnly()
  277. {
  278. return View();
  279. }
  280.  
  281. public ActionResult UnAuthorized()
  282. {
  283. return View();
  284. }
  285.  
  286. [AuthorizeRoles("Admin")]
  287. public ActionResult ManageUserPartial()
  288. {
  289. if (User.Identity.IsAuthenticated)
  290. {
  291. string loginName = User.Identity.Name;
  292. UserManager UM = new UserManager();
  293. UserDataView UDV = UM.GetUserDataView(loginName);
  294. return PartialView(UDV);
  295. }
  296. return View();
  297. }
  298.  
  299. public ActionResult About()
  300. {
  301. ViewBag.Message = "Your application description page.";
  302.  
  303. return View();
  304. }
  305.  
  306. public ActionResult Contact()
  307. {
  308. ViewBag.Message = "Your contact page.";
  309.  
  310. return View();
  311. }
  312. }
  313. }
  314.  
  315. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  316. {
  317.  
  318. SYSUser SU = new SYSUser();
  319. SU.LoginName = user.LoginName;
  320. SU.PasswordEncryptedText = user.Password;
  321. SU.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
  322. SU.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; ;
  323. SU.RowCreatedDateTime = DateTime.Now;
  324. SU.RowMOdifiedDateTime = DateTime.Now;
  325.  
  326. db.SYSUsers.Add(SU);
  327. db.SaveChanges();
  328.  
  329. SYSUserProfile SUP = new SYSUserProfile();
  330. SUP.SYSUserID = SU.SYSUserID;
  331. SUP.FirstName = user.FirstName;
  332. SUP.LastName = user.LastName;
  333. SUP.Gender = user.Gender;
  334. SUP.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
  335. SUP.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
  336. SUP.RowCreatedDateTime = DateTime.Now;
  337. SUP.RowModifiedDateTime = DateTime.Now;
  338.  
  339. db.SYSUserProfiles.Add(SUP);
  340. db.SaveChanges();
  341.  
  342.  
  343. if (user.LOOKUPRoleID > 0)
  344. {
  345. SYSUserRole SUR = new SYSUserRole();
  346. SUR.LOOKUPRoleID = user.LOOKUPRoleID;
  347. SUR.SYSUserID = user.SYSUserID;
  348. SUR.IsActive = true;
  349. SUR.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
  350. SUR.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
  351. SUR.RowCreatedDateTime = DateTime.Now;
  352. SUR.RowModifiedDateTime = DateTime.Now;
  353.  
  354. db.SYSUserRoles.Add(SUR);
  355. db.SaveChanges();
  356. }
  357. }
  358. }
  359.  
  360. public bool IsLoginNameExist(string loginName)
  361. {
  362. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  363. {
  364. return db.SYSUsers.Where(o => o.LoginName.Equals(loginName)).Any();
  365. }
  366. }
  367.  
  368. public string GetUserPassword(string loginName)
  369. {
  370. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  371. {
  372. var user = db.SYSUsers.Where(o => o.LoginName.ToLower().Equals(loginName));
  373. if (user.Any())
  374. return user.FirstOrDefault().PasswordEncryptedText;
  375. else
  376. return string.Empty;
  377. }
  378. }
  379.  
  380. public bool IsUserInRole(string loginName, string roleName)
  381. {
  382. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  383. {
  384. SYSUser SU = db.SYSUsers.Where(o => o.LoginName.ToLower().Equals(loginName))?.FirstOrDefault();
  385. if (SU != null)
  386. {
  387. var roles = from q in db.SYSUserRoles
  388. join r in db.LOOKUPRoles on q.LOOKUPRoleID equals r.LOOKUPRoleID
  389. where r.RoleName.Equals(roleName) && q.SYSUserID.Equals(SU.SYSUserID)
  390. select r.RoleName;
  391.  
  392. if (roles != null)
  393. {
  394. return roles.Any();
  395. }
  396. }
  397.  
  398. return false;
  399. }
  400. }
  401.  
  402. public List<LOOKUPAvailableRole> GetAllRoles()
  403. {
  404. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  405. {
  406. var roles = db.LOOKUPRoles.Select(o => new LOOKUPAvailableRole
  407. {
  408. LOOKUPRoleID = o.LOOKUPRoleID,
  409. RoleName = o.RoleName,
  410. RoleDescription = o.RoleDescription
  411. }).ToList();
  412.  
  413. return roles;
  414. }
  415. }
  416.  
  417. public int GetUserID(string loginName)
  418. {
  419. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  420. {
  421. var user = db.SYSUsers.Where(o => o.LoginName.Equals(loginName));
  422. if (user.Any()) return user.FirstOrDefault().SYSUserID;
  423. }
  424. return 0;
  425. }
  426. public List<UserProfileView> GetAllUserProfiles()
  427. {
  428. List<UserProfileView> profiles = new List<UserProfileView>();
  429. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  430. {
  431. UserProfileView UPV;
  432. var users = db.SYSUsers.ToList();
  433.  
  434. foreach (SYSUser u in db.SYSUsers)
  435. {
  436. UPV = new UserProfileView();
  437. UPV.SYSUserID = u.SYSUserID;
  438. UPV.LoginName = u.LoginName;
  439. UPV.Password = u.PasswordEncryptedText;
  440.  
  441. var SUP = db.SYSUserProfiles.Find(u.SYSUserID);
  442. if (SUP != null)
  443. {
  444. UPV.FirstName = SUP.FirstName;
  445. UPV.LastName = SUP.LastName;
  446. UPV.Gender = SUP.Gender;
  447. }
  448.  
  449. var SUR = db.SYSUserRoles.Where(o => o.SYSUserID.Equals(u.SYSUserID));
  450. if (SUR.Any())
  451. {
  452. var userRole = SUR.FirstOrDefault();
  453. UPV.LOOKUPRoleID = userRole.LOOKUPRoleID;
  454. UPV.RoleName = userRole.LOOKUPRole.RoleName;
  455. UPV.IsRoleActive = userRole.IsActive;
  456. }
  457.  
  458. profiles.Add(UPV);
  459. }
  460. }
  461.  
  462. return profiles;
  463. }
  464.  
  465. public UserDataView GetUserDataView(string loginName)
  466. {
  467. UserDataView UDV = new UserDataView();
  468. List<UserProfileView> profiles = GetAllUserProfiles();
  469. List<LOOKUPAvailableRole> roles = GetAllRoles();
  470.  
  471. int? userAssignedRoleID = 0, userID = 0;
  472. string userGender = string.Empty;
  473.  
  474. userID = GetUserID(loginName);
  475. using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
  476. {
  477. userAssignedRoleID = db.SYSUserRoles.Where(o => o.SYSUserID == userID)?.FirstOrDefault().LOOKUPRoleID;
  478. userGender = db.SYSUserProfiles.Where(o => o.SYSUserID == userID)?.FirstOrDefault().Gender;
  479. }
  480.  
  481. List<Gender> genders = new List<Gender>();
  482. genders.Add(new Gender
  483. {
  484. Text = "Male",
  485. Value = "M"
  486. });
  487. genders.Add(new Gender
  488. {
  489. Text = "Female",
  490. Value = "F"
  491. });
  492.  
  493. UDV.UserProfile = profiles;
  494. UDV.UserRoles = new UserRoles
  495. {
  496. SelectedRoleID = userAssignedRoleID,
  497. UserRoleList = roles
  498. };
  499. UDV.UserGender = new UserGender
  500. {
  501. SelectedGender = userGender,
  502. Gender = genders
  503. };
  504. return UDV;
  505. }
  506. } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement