/// <summary> /// Change email. /// </summary> /// <param name="email">Email</param> /// <param name="userId">Id</param> /// <param name="password">Password</param> /// <returns>Status</returns> public string ChangeEmail(string email, string userId, string password) { try { var user = IdentityUnitOfWork.UserManager.FindById(userId); var test = IdentityUnitOfWork.UserManager.FindByEmail(email); if (test == null && user != null && IdentityUnitOfWork.UserManager.CheckPassword(user, password)) { user.EmailConfirmed = false; user.Email = email; user.UserName = email; IdentityUnitOfWork.UserManager.Update(user); IdentityUnitOfWork.Save(); return(""); } else { return("Неверный пароль или пользователь с такой почтой уже существует."); } } catch (Exception e) { CreateLog(e, "AccountService", "ChangeEmail"); return("Неверный пароль или пользователь с такой почтой уже существует."); } }
/// <summary> /// Unban user. /// </summary> /// <param name="userId">User id</param> public void UnbanUser(string userId) { try { IdentityUnitOfWork.UserManager.RemoveFromRole(userId, "banned"); IdentityUnitOfWork.Save(); } catch (Exception e) { CreateLog(e, "AdminService", "UnbanUserAsync"); } }
/// <summary> /// Update information about client. /// </summary> /// <param name="profileDTO">New profile object</param> public void UpdateProfile(ProfileDTO profileDTO) { try { profileDTO.Id = profileDTO.User.Id; Profile profile = IdentityMapperDTO.ToProfile.Map <ProfileDTO, Profile>(profileDTO); profile.ApplicationUser = IdentityUnitOfWork.UserManager.FindById(profileDTO.User.Id); IdentityUnitOfWork.ClientManager.Update(profile); IdentityUnitOfWork.Save(); } catch (Exception e) { CreateLog(e, "ClientService", "UpdateProfileAsync"); } }
/// <summary> /// Confirm user email. /// </summary> /// <param name="userId">Id</param> public void ConfirmEmail(string userId) { try { var user = IdentityUnitOfWork.UserManager.FindById(userId); if (user != null) { user.EmailConfirmed = true; IdentityUnitOfWork.UserManager.Update(user); IdentityUnitOfWork.Save(); } } catch (Exception e) { CreateLog(e, "AccountService", "ConfirmEmailAsync"); } }
/// <summary> /// Ban user. /// </summary> /// <param name="userId">User id</param> public void BanUser(string userId) { try { var role = IdentityUnitOfWork.RoleManager.FindByName("banned"); if (role == null) { role = new ApplicationRole { Name = "banned" }; IdentityUnitOfWork.RoleManager.Create(role); } IdentityUnitOfWork.UserManager.AddToRole(userId, "banned"); IdentityUnitOfWork.Save(); } catch (Exception e) { CreateLog(e, "AdminService", "BanUserAsync"); } }
/// <summary> /// Change password. /// </summary> /// <param name="userId">Id</param> /// <param name="password">Password</param> /// <returns>Status</returns> public string ChangePassword(string userId, string password) { try { var user = IdentityUnitOfWork.UserManager.FindById(userId); if (user != null) { IdentityUnitOfWork.UserManager.RemovePassword(userId); IdentityUnitOfWork.UserManager.AddPassword(userId, password); IdentityUnitOfWork.Save(); return(""); } else { return("Неверный пароль."); } } catch (Exception e) { CreateLog(e, "AccountService", "ChangeName"); return("Неверный пароль"); } }
/// <summary> /// Change name. /// </summary> /// <param name="name">Name</param> /// <param name="userId">Id</param> /// <param name="password">Password</param> /// <returns>Status</returns> public string ChangeName(string name, string userId, string password) { try { var user = IdentityUnitOfWork.UserManager.FindById(userId); if (user != null && IdentityUnitOfWork.UserManager.CheckPassword(user, password)) { user.Name = name; IdentityUnitOfWork.UserManager.Update(user); IdentityUnitOfWork.Save(); return(""); } else { return("Неверный пароль."); } } catch (Exception e) { CreateLog(e, "AccountService", "ChangeName"); return("Неверный пароль"); } }