public override string[] GetRolesForUser(string username) { if (username == null) throw new ArgumentNullException("username"); if (username == "") throw new ArgumentException("An argument can't be empty", "username"); DiamondsEntities db = new DiamondsEntities(); List<string> roles = new List<string>(); User user = db.Users.FirstOrDefault(u => u.email == username); if (user == null) return roles.ToArray(); RoleGroup roleGroup = user.RoleGroup; foreach (rolegroup_role rgr in roleGroup.rolegroup_role) if (rgr.value) roles.Add(rgr.Role.code); else roles.Remove(rgr.Role.code); foreach (user_role ur in user.user_role) if (ur.value) roles.Add(ur.Role.code); else roles.Remove(ur.Role.code); return roles.ToArray(); }
/// <summary> /// Sprawdza czy hasło i użytkownik się zgadzają /// </summary> /// <param name="username">Nazwa użytkownika</param> /// <param name="password">Hasło użytkownika</param> /// <returns>Prawda jeżeli wszystko się zgadza</returns> public override bool ValidateUser(string username, string password) { DiamondsEntities db = new DiamondsEntities(); User user = db.Users.FirstOrDefault(u => u.email == username); if (user != null && user.checkPassword(password)) { user.lastLoginDate = DateTime.Now; db.SaveChanges(); return true; } return false; }
/// <summary> /// Funkcja zwraca użytkownika o określonym ID /// </summary> /// <param name="providerUserKey">ID użytkownika</param> /// <param name="userIsOnline">Nieważne</param> /// <returns>Zwraca użytkownika</returns> public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) { DiamondsEntities db = new DiamondsEntities(); User user = db.Users.First(u => u.id == (int)providerUserKey); if (user != null) { MembershipUser memUser = new MembershipUser("DMembershipProvider", user.name, user.id, user.email, string.Empty, string.Empty, user.isConfirmed, false, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue); return memUser; } return null; }
/// <summary> /// Zwraca wszystkich użytkowników, określonych przez paginację /// </summary> /// <param name="pageIndex">Nr strony</param> /// <param name="pageSize">Ilość uzytkownikow na stronie</param> /// <param name="totalRecords">Ilość wszystkich użytkowników</param> /// <returns>Kolekcja użytkowników</returns> public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) { MembershipUserCollection userColl = new MembershipUserCollection(); DiamondsEntities db = new DiamondsEntities(); totalRecords = db.Users.Count(); foreach (User user in db.Users.OrderBy(u => u.name).Skip(pageIndex * pageSize).Take(pageSize)) { userColl.Add(new MembershipUser("DMembershipProvider", user.name, user.id, user.email, string.Empty, string.Empty, user.isConfirmed, false, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue)); } return userColl; }
/// <summary> /// Tworzy użytkownika /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="email"></param> /// <param name="passwordQuestion"></param> /// <param name="passwordAnswer"></param> /// <param name="isApproved"></param> /// <param name="providerUserKey"></param> /// <param name="status"></param> /// <returns></returns> public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true); OnValidatingPassword(args); if (args.Cancel) { status = MembershipCreateStatus.InvalidPassword; return null; } if (RequiresUniqueEmail && GetUserNameByEmail(email) != string.Empty) { status = MembershipCreateStatus.DuplicateEmail; return null; } MembershipUser user = GetUser(username, true); if (user == null) { DiamondsEntities db = new DiamondsEntities(); User newUser = new User(); newUser.name = username; newUser.setPassword(password); newUser.email = email; newUser.createDate = newUser.lastLoginDate = DateTime.Now; newUser.isConfirmed = isApproved; db.Users.Add(newUser); db.SaveChanges(); status = MembershipCreateStatus.Success; return GetUser(username, true); } else { status = MembershipCreateStatus.DuplicateUserName; } return null; }
public override bool ChangePassword(string username, string oldPassword, string newPassword) { DiamondsEntities db = new DiamondsEntities(); User user = db.Users.FirstOrDefault(u => u.email == username || u.name == username); if (!user.checkPassword(oldPassword)) return false; else { user.setPassword(newPassword); db.SaveChanges(); } return true; }
public override bool IsUserInRole(string username, string roleName) { DiamondsEntities db = new DiamondsEntities(); User user = db.Users.FirstOrDefault(u => u.email == username); if (user == null) return false; return user.hasAccess(roleName); }
public ActionResult RoleEdit(int id, Role role, List<RoleAccess> roleAccesses) { if (ModelState.IsValid) { db.Roles.Attach(role); db.Entry(role).State = EntityState.Modified; db.SaveChanges(); db = new DiamondsEntities(); role = db.Roles.Single(r => r.roleId == id); role.rolegroup_role.Clear(); foreach (var group in roleAccesses) role.rolegroup_role.Add(group.ToRoleGroupRole()); db.SaveChanges(); return RedirectToAction("RoleEdit", new { id = role.roleId }); } ViewBag.RoleAccess = getGroupsAccess(role); return View(role); }