public ActionResult CurrentUserEdit(int id, SuncereUser model, string OldPassword) { AjaxResult result; try { FluentModel db = Session.GetFluentModel(); SuncereUserRepository repository = new SuncereUserRepository(db); SuncereUser user = repository.FirstOrDefault(id); if (!string.IsNullOrEmpty(model.Password)) { if (OldPassword == AsymmetricEncryption.Default.Decrypt(user.Password)) { user.Password = AsymmetricEncryption.Default.Encrypt(model.Password); } else { throw new Exception("旧密码错误。"); } } user.EmailAddress = model.EmailAddress; user.PhoneNumber = model.PhoneNumber; user.Remark = model.Remark; user.LastModifierUserId = id; db.SaveChanges(); result = AjaxResult.GetEditAjaxResult(true); } catch (Exception e) { result = AjaxResult.GetEditAjaxResult(false); } return(Json(result, JsonRequestBehavior.DenyGet)); }
public ActionResult UserEdit(int id) { FluentModel db = Session.GetFluentModel(); SuncereUserRepository repository = new SuncereUserRepository(db); List <SuncereRole> list = db.SuncereRoles.Where(o => o.Status).ToList(); ViewData["SuncereRoles"] = list; return(View(repository.FirstOrDefault(id))); }
public ActionResult Login2(string cipher, string returnUrl) { AjaxResult ar; try { string text = AsymmetricEncryption.Default.Decrypt(cipher); LoginInfo li = JsonConvert.DeserializeObject <LoginInfo>(text); if (string.IsNullOrEmpty(li.UserName)) { throw new Exception("请输入用户名"); } if (string.IsNullOrEmpty(li.Password)) { throw new Exception("请输入密码"); } FluentModel db = Session.GetFluentModel(); SuncereUserRepository userRepository = new SuncereUserRepository(db); SuncereUser user = userRepository.FirstOrDefault(li.UserName, true); if (user == null) { throw new Exception("用户名不存在或已停用,请核对后重新登录"); } if (AsymmetricEncryption.Default.Decrypt(user.Password) != li.Password) { throw new Exception("密码错误,请核对后重新登录"); } user.LastLoginTime = DateTime.Now; user.LastLoginHostAddress = Request.UserHostAddress; db.SaveChanges(); Session.SetCurrentUser(user); List <SuncerePermission> userPermissions = new List <SuncerePermission>(); foreach (SuncereRole role in user.SuncereRoles.Where(o => o.Status)) { foreach (SuncerePermission permission in role.SuncerePermissions.Where(o => o.Status)) { if (!userPermissions.Contains(permission)) { userPermissions.Add(permission); } } } Session.SetUserPermissions(userPermissions); ar = AjaxResult.GetLoginAjaxResult(true); } catch (Exception e) { ar = new AjaxResult(false, e.Message); } return(Json(ar, JsonRequestBehavior.DenyGet)); }
public ActionResult Login(string userName, string password, string returnUrl) { try { if (string.IsNullOrEmpty(userName)) { throw new Exception("请输入用户名"); } if (string.IsNullOrEmpty(password)) { throw new Exception("请输入密码"); } FluentModel db = Session.GetFluentModel(); SuncereUserRepository userRepository = new SuncereUserRepository(db); SuncereUser user = userRepository.FirstOrDefault(userName, true); if (user == null) { throw new Exception("用户名不存在或已停用,请核对后重新登录"); } if (AsymmetricEncryption.Default.Decrypt(user.Password) != password) { throw new Exception("密码错误,请核对后重新登录"); } user.LastLoginTime = DateTime.Now; user.LastLoginHostAddress = Request.UserHostAddress; db.SaveChanges(); Session.SetCurrentUser(user); List <SuncerePermission> userPermissions = new List <SuncerePermission>(); foreach (SuncereRole role in user.SuncereRoles.Where(o => o.Status)) { foreach (SuncerePermission permission in role.SuncerePermissions.Where(o => o.Status)) { if (!userPermissions.Contains(permission)) { userPermissions.Add(permission); } } } Session.SetUserPermissions(userPermissions); return(Redirect(returnUrl)); } catch (Exception e) { ViewData["message"] = e.Message; return(View()); } }
public ActionResult UserEnable(int id, bool status) { AjaxResult result; try { FluentModel db = Session.GetFluentModel(); SuncereUserRepository repository = new SuncereUserRepository(db); SuncereUser role = repository.FirstOrDefault(id); role.Status = status; role.LastModifierUserId = Session.GetCurrentUser().Id; db.SaveChanges(); result = AjaxResult.GetEditAjaxResult(true); } catch (Exception e) { result = AjaxResult.GetEditAjaxResult(false); } return(Json(result, JsonRequestBehavior.DenyGet)); }
public ActionResult UserEdit(int id, SuncereUser model) { AjaxResult result; try { int[] roleIds = Request.Params["SuncereRoles"].Split(',').Select(o => int.Parse(o)).ToArray(); FluentModel db = Session.GetFluentModel(); SuncereUserRepository repository = new SuncereUserRepository(db); SuncereRoleRepository roleRepository = new SuncereRoleRepository(db); SuncereUser user = repository.FirstOrDefault(id); if (!string.IsNullOrEmpty(model.Password)) { user.Password = AsymmetricEncryption.Default.Encrypt(model.Password); } user.EmailAddress = model.EmailAddress; user.PhoneNumber = model.PhoneNumber; foreach (SuncereRole role in user.SuncereRoles) { role.SuncereUsers.Remove(user); } user.SuncereRoles.Clear(); foreach (int roleId in roleIds) { SuncereRole role = roleRepository.FirstOrDefault(roleId); user.SuncereRoles.Add(role); role.SuncereUsers.Add(user); } user.Remark = model.Remark; user.LastModifierUserId = Session.GetCurrentUser().Id; db.SaveChanges(); result = AjaxResult.GetEditAjaxResult(true); } catch (Exception e) { result = AjaxResult.GetEditAjaxResult(false); } return(Json(result, JsonRequestBehavior.DenyGet)); }