Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BucketListApplication.Models.DB;
- using BucketListApplication.Models.EntityManager;
- using BucketListApplication.Models.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace BucketListApplication.Controllers
- {
- public class ListController : Controller
- {
- #region ToDolIst View and Post for AddToList
- public ActionResult ToDoList()
- {
- return View();
- }
- // In the post I pass in the viewmodel that the view will use to collect information
- [HttpPost]
- public ActionResult ToDoList(BucketListView BLV)
- {
- if (ModelState.IsValid)
- {
- ListManager LM = new ListManager();
- // Addtolist is a method belonging to ListManager and can only be called if ListManager
- // object is created in the current context.
- LM.addToList(BLV);
- return RedirectToAction("MyToDoList", "Home");
- }
- return View("Welcome", "Home");
- }
- #endregion
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- namespace BucketListApplication.Models.ViewModel
- {
- public class BucketListView
- {
- [Key]
- public int UserBucketListID { get; set; }
- public int SYSUserProfileID { get; set; }
- public Nullable<System.DateTime> BucketItemDate { get; set; }
- public string BucketItem { get; set; }
- public int BucketImportance { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using BucketListApplication.Models.DB;
- using BucketListApplication.Models.ViewModel;
- namespace BucketListApplication.Models.EntityManager
- {
- public class ListManager
- {
- public void addToList(BucketListView BLV)
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- UserBucketList UBL = new UserBucketList();
- UBL.SYSUserProfileID = BLV.SYSUserProfileID;
- UBL.BucketItemDate = BLV.BucketItemDate;
- UBL.BucketItem = BLV.BucketItem;
- UBL.BucketImportance = BLV.BucketImportance;
- UBL.RowCreatedSYSUserID = BLV.SYSUserProfileID > 0 ? BLV.SYSUserProfileID : 1;
- UBL.RowModifiedSYSUserID = BLV.SYSUserProfileID > 0 ? BLV.SYSUserProfileID : 1; ;
- UBL.RowCreatedDateTime = DateTime.Now;
- UBL.RowModifiedDateTime = DateTime.Now;
- db.UserBucketLists.Add(UBL);
- db.SaveChanges();
- }
- }
- }
- }
- @model IEnumerable<BucketListApplication.Models.DB.UserBucketList>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.BucketItemDate)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.BucketItem)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.BucketImportance)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.RowCreatedSYSUserID)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.RowCreatedDateTime)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.RowModifiedSYSUserID)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.RowModifiedDateTime)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.SYSUserProfile.FirstName)
- </th>
- <th></th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.BucketItemDate)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.BucketItem)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.BucketImportance)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.RowCreatedSYSUserID)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.RowCreatedDateTime)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.RowModifiedSYSUserID)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.RowModifiedDateTime)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.SYSUserProfile.FirstName)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.UserBucketListID }) |
- @Html.ActionLink("Details", "Details", new { id=item.UserBucketListID }) |
- @Html.ActionLink("Delete", "Delete", new { id=item.UserBucketListID })
- </td>
- </tr>
- }
- </table>
- <div>
- <a href="@Url.Action("ToDoList", "List")" class="Button">Add More Items</a>
- </div>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using BucketListApplication.Models.ViewModel;
- using BucketListApplication.Models.EntityManager;
- using System.Web.Security;
- namespace BucketListApplication.Controllers
- {
- public class AccountController : Controller
- {
- public ActionResult SignUp()
- {
- return View();
- }
- [HttpPost]
- public ActionResult SignUp(UserSignUpView USV)
- {
- if (ModelState.IsValid)
- {
- UserManager UM = new UserManager();
- if (!UM.IsLoginNameExist(USV.LoginName))
- {
- UM.AddUserAccount(USV);
- FormsAuthentication.SetAuthCookie(USV.FirstName, false);
- return RedirectToAction("Welcome", "Home");
- }
- else
- ModelState.AddModelError("", "Login Name already taken.");
- }
- return View();
- }
- public ActionResult LogIn()
- {
- return View();
- }
- [HttpPost]
- public ActionResult LogIn(UserLoginView ULV, string returnUrl)
- {
- if (ModelState.IsValid)
- {
- UserManager UM = new UserManager();
- string password = UM.GetUserPassword(ULV.LoginName);
- if (string.IsNullOrEmpty(password))
- ModelState.AddModelError("", "The user login or password provided is incorrect.");
- else
- {
- if (ULV.Password.Equals(password))
- {
- FormsAuthentication.SetAuthCookie(ULV.LoginName, false);
- return RedirectToAction("Welcome", "Home");
- }
- else
- {
- ModelState.AddModelError("", "The password provided is incorrect.");
- }
- }
- }
- // If we got this far, something failed, redisplay form
- return View(ULV);
- }
- [Authorize]
- public ActionResult SignOut()
- {
- FormsAuthentication.SignOut();
- return RedirectToAction("Index", "Home");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Security;
- using BucketListApplication.Models.ViewModel;
- using BucketListApplication.Models.EntityManager;
- using BucketListApplication.Security;
- using BucketListApplication.Models.DB;
- namespace BucketListApplication.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- [Authorize]
- public ActionResult Welcome()
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- return View(db.UserBucketLists.ToList());
- }
- }
- [AuthorizeRoles("Admin")]
- public ActionResult AdminOnly()
- {
- return View();
- }
- public ActionResult UnAuthorized()
- {
- return View();
- }
- [AuthorizeRoles("Admin")]
- public ActionResult ManageUserPartial()
- {
- if (User.Identity.IsAuthenticated)
- {
- string loginName = User.Identity.Name;
- UserManager UM = new UserManager();
- UserDataView UDV = UM.GetUserDataView(loginName);
- return PartialView(UDV);
- }
- return View();
- }
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
- return View();
- }
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
- return View();
- }
- }
- }
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- SYSUser SU = new SYSUser();
- SU.LoginName = user.LoginName;
- SU.PasswordEncryptedText = user.Password;
- SU.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
- SU.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; ;
- SU.RowCreatedDateTime = DateTime.Now;
- SU.RowMOdifiedDateTime = DateTime.Now;
- db.SYSUsers.Add(SU);
- db.SaveChanges();
- SYSUserProfile SUP = new SYSUserProfile();
- SUP.SYSUserID = SU.SYSUserID;
- SUP.FirstName = user.FirstName;
- SUP.LastName = user.LastName;
- SUP.Gender = user.Gender;
- SUP.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
- SUP.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
- SUP.RowCreatedDateTime = DateTime.Now;
- SUP.RowModifiedDateTime = DateTime.Now;
- db.SYSUserProfiles.Add(SUP);
- db.SaveChanges();
- if (user.LOOKUPRoleID > 0)
- {
- SYSUserRole SUR = new SYSUserRole();
- SUR.LOOKUPRoleID = user.LOOKUPRoleID;
- SUR.SYSUserID = user.SYSUserID;
- SUR.IsActive = true;
- SUR.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
- SUR.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1;
- SUR.RowCreatedDateTime = DateTime.Now;
- SUR.RowModifiedDateTime = DateTime.Now;
- db.SYSUserRoles.Add(SUR);
- db.SaveChanges();
- }
- }
- }
- public bool IsLoginNameExist(string loginName)
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- return db.SYSUsers.Where(o => o.LoginName.Equals(loginName)).Any();
- }
- }
- public string GetUserPassword(string loginName)
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- var user = db.SYSUsers.Where(o => o.LoginName.ToLower().Equals(loginName));
- if (user.Any())
- return user.FirstOrDefault().PasswordEncryptedText;
- else
- return string.Empty;
- }
- }
- public bool IsUserInRole(string loginName, string roleName)
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- SYSUser SU = db.SYSUsers.Where(o => o.LoginName.ToLower().Equals(loginName))?.FirstOrDefault();
- if (SU != null)
- {
- var roles = from q in db.SYSUserRoles
- join r in db.LOOKUPRoles on q.LOOKUPRoleID equals r.LOOKUPRoleID
- where r.RoleName.Equals(roleName) && q.SYSUserID.Equals(SU.SYSUserID)
- select r.RoleName;
- if (roles != null)
- {
- return roles.Any();
- }
- }
- return false;
- }
- }
- public List<LOOKUPAvailableRole> GetAllRoles()
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- var roles = db.LOOKUPRoles.Select(o => new LOOKUPAvailableRole
- {
- LOOKUPRoleID = o.LOOKUPRoleID,
- RoleName = o.RoleName,
- RoleDescription = o.RoleDescription
- }).ToList();
- return roles;
- }
- }
- public int GetUserID(string loginName)
- {
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- var user = db.SYSUsers.Where(o => o.LoginName.Equals(loginName));
- if (user.Any()) return user.FirstOrDefault().SYSUserID;
- }
- return 0;
- }
- public List<UserProfileView> GetAllUserProfiles()
- {
- List<UserProfileView> profiles = new List<UserProfileView>();
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- UserProfileView UPV;
- var users = db.SYSUsers.ToList();
- foreach (SYSUser u in db.SYSUsers)
- {
- UPV = new UserProfileView();
- UPV.SYSUserID = u.SYSUserID;
- UPV.LoginName = u.LoginName;
- UPV.Password = u.PasswordEncryptedText;
- var SUP = db.SYSUserProfiles.Find(u.SYSUserID);
- if (SUP != null)
- {
- UPV.FirstName = SUP.FirstName;
- UPV.LastName = SUP.LastName;
- UPV.Gender = SUP.Gender;
- }
- var SUR = db.SYSUserRoles.Where(o => o.SYSUserID.Equals(u.SYSUserID));
- if (SUR.Any())
- {
- var userRole = SUR.FirstOrDefault();
- UPV.LOOKUPRoleID = userRole.LOOKUPRoleID;
- UPV.RoleName = userRole.LOOKUPRole.RoleName;
- UPV.IsRoleActive = userRole.IsActive;
- }
- profiles.Add(UPV);
- }
- }
- return profiles;
- }
- public UserDataView GetUserDataView(string loginName)
- {
- UserDataView UDV = new UserDataView();
- List<UserProfileView> profiles = GetAllUserProfiles();
- List<LOOKUPAvailableRole> roles = GetAllRoles();
- int? userAssignedRoleID = 0, userID = 0;
- string userGender = string.Empty;
- userID = GetUserID(loginName);
- using (ToDoListApplicationDBEntities db = new ToDoListApplicationDBEntities())
- {
- userAssignedRoleID = db.SYSUserRoles.Where(o => o.SYSUserID == userID)?.FirstOrDefault().LOOKUPRoleID;
- userGender = db.SYSUserProfiles.Where(o => o.SYSUserID == userID)?.FirstOrDefault().Gender;
- }
- List<Gender> genders = new List<Gender>();
- genders.Add(new Gender
- {
- Text = "Male",
- Value = "M"
- });
- genders.Add(new Gender
- {
- Text = "Female",
- Value = "F"
- });
- UDV.UserProfile = profiles;
- UDV.UserRoles = new UserRoles
- {
- SelectedRoleID = userAssignedRoleID,
- UserRoleList = roles
- };
- UDV.UserGender = new UserGender
- {
- SelectedGender = userGender,
- Gender = genders
- };
- return UDV;
- }
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement