/// <summary> /// Create a new user for the system /// </summary> /// <param name="model">Model</param> /// <returns>MembershipCreateStatus</returns> public MembershipCreateStatus SaveUser(UserModel model) { MembershipCreateStatus status; _provider.CreateUser(model.Email, model.Password, model.Email, null, null, true, null, out status); if (status == MembershipCreateStatus.Success) { UserProfile profile = UserProfile.GetUserProfile(model.Email); profile.UserType = model.SelectedUserType.ToString(); profile.Save(); if (model.SelectedUserType > 0 && model.SelectedUserType != 4) { switch (model.SelectedUserType) { case 1: Roles.AddUserToRole(model.Email, "GlobalAdministrator"); break; case 3: Roles.AddUserToRole(model.Email, "Client"); break; } } } return status; }
/// <summary> /// Perform the update operaton when a permission is updated /// </summary> /// <param name="model">User model</param> /// <param name="loggedUser">Logged user</param> /// <param name="context">Database context</param> /// <param name="exists">Exist or not exist the user</param> private static void UpdateBusinessApplicationUser(UserModel model, string loggedUser, VestalisEntities context, bool exists) { VestalisUserApplication userApplication = (from userApp in context.VestalisUserApplications where userApp.IsDeleted == false && userApp.UserName == model.Email && userApp.BusinessApplicationId == model.BusinessApplicationId select userApp).FirstOrDefault(); if (userApplication != null && userApplication.BusinessApplicationId == model.BusinessApplicationIdPer) { userApplication.BusinessApplicationId = model.BusinessApplicationIdPer; userApplication.ClientId = model.ClientId; userApplication.ModificationBy = loggedUser; userApplication.CreationDate = DateTime.UtcNow; context.SaveChanges(); } else if (userApplication != null && userApplication.BusinessApplicationId != model.BusinessApplicationIdPer) { if (!exists) { userApplication.BusinessApplicationId = model.BusinessApplicationIdPer; userApplication.ClientId = model.ClientId; userApplication.ModificationBy = loggedUser; userApplication.CreationDate = DateTime.UtcNow; context.SaveChanges(); } else { model.ErrorList.Add(LanguageResource.UserInBusinessApplication); } } }
/// <summary> /// Verify and save VestalisUserApplications /// </summary> /// <param name="model">Model</param> /// <param name="loggedUser">Logged user</param> public static void VerifySaveVestalisUserApplications(UserModel model, string loggedUser) { using (VestalisEntities context = new VestalisEntities()) { //if the selected business application is already added to the user, the system will show an error message //otherwise the system continues with the process bool exists = (from userApp in context.VestalisUserApplications where userApp.IsDeleted == false && userApp.UserName == model.Email && userApp.BusinessApplicationId == model.BusinessApplicationId select userApp).Any(); if (model.OpenMode == (int)ScreenOpenMode.Add) { if (!exists) { VestalisUserApplication userApplication = new VestalisUserApplication { BusinessApplicationId = model.BusinessApplicationId, UserName = model.Email, ClientId = model.ClientId, CreationBy = loggedUser, CreationDate = DateTime.UtcNow }; context.VestalisUserApplications.AddObject(userApplication); context.SaveChanges(); } else { model.ErrorList.Add(LanguageResource.UserInBusinessApplication); } } else if (model.OpenMode == (int)ScreenOpenMode.Edit) { UpdateBusinessApplicationUser(model, loggedUser, context, exists); } } }
/// <summary> /// Edit the information and password of a user /// </summary> /// <param name="model">Model</param> public void EditUser(UserModel model) { MembershipUser currentUser = _provider.GetUser(model.Email, true); currentUser.Email = model.Email; _provider.UpdateUser(currentUser); if (!string.IsNullOrEmpty(model.Password)) { string tempPassword = currentUser.ResetPassword(); _provider.ChangePassword(model.Email, tempPassword, model.Password); } }
/// <summary> /// Perform a validation of the user form before save /// </summary> /// <param name="model">User model</param> private void ValidateUserSave(UserModel model) { if (model.OpenMode == (int)ScreenOpenMode.Add) { //regular expression for validate e-mails Regex regEx = new Regex("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); //validations for the field e/mail if (string.IsNullOrEmpty(model.Email))//validate mandatory model.ErrorList.Add(Resources.Administration.EmailMandatory); if (!string.IsNullOrEmpty(model.Email) && !regEx.IsMatch(model.Email))//validate format model.ErrorList.Add(Resources.Administration.EmailFormatNotValid); //validations for the field password if (string.IsNullOrEmpty(model.Password))//validate mandatory model.ErrorList.Add(Resources.Administration.PasswordNotFilled); if (!string.IsNullOrEmpty(model.Password) && model.Password.Length < 6)//validate lenght model.ErrorList.Add(Resources.Administration.MinLegthPassword); //validation for mandatory business application if (model.BusinessApplicationId == Guid.Empty && model.SelectedUserType != 1) model.ErrorList.Add(Resources.Administration.BusinessApplicationNotSelected); //validation for client field if (model.SelectedUserType == 3 && model.ClientId == null) model.ErrorList.Add(Resources.Administration.ClientNotSelected); //validation for selected roles else if (model.SelectedUserType == 4 && string.IsNullOrEmpty(model.CheckedRoles)) model.ErrorList.Add(Resources.Administration.RolesNotSelected); } else if (model.OpenMode == (int)ScreenOpenMode.Edit) { //validations for the field password if (string.IsNullOrEmpty(model.Password))//validate mandatory model.ErrorList.Add(Resources.Administration.PasswordNotFilled); if (!string.IsNullOrEmpty(model.Password) && model.Password.Length < 6)//validate lenght model.ErrorList.Add(Resources.Administration.MinLegthPassword); } }
/// <summary> /// Add the selected roles for the user /// </summary> /// <param name="model">UserModel</param> public static void SaveRolesForCotecnaUser(UserModel model) { string[] selectedRoles = model.CheckedRoles.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (model.OpenMode == (int)ScreenOpenMode.Add) { foreach (string role in selectedRoles) { try { Roles.AddUserToRole(model.Email, role); } catch (Exception) { model.ErrorList.Add(string.Format(LanguageResource.UserExistInRole, role)); } if (model.HasErrors) { Roles.RemoveUserFromRoles(model.Email, selectedRoles); } } } else if (model.OpenMode == (int)ScreenOpenMode.Edit) { string[] currentBusinessAppRoles = Roles.GetRolesForUser(model.Email); string prefix = "_"; prefix += GetAllBusinessAplications().FirstOrDefault(data => data.BusinessApplicationId == model.BusinessApplicationId).Prefix; currentBusinessAppRoles = currentBusinessAppRoles.Where(data => data.Contains(prefix)).ToArray(); Roles.RemoveUserFromRoles(model.Email, currentBusinessAppRoles); foreach (string role in selectedRoles) { Roles.AddUserToRole(model.Email, role); } } }
/// <summary> /// Perform a validation when a permission is saved /// </summary> /// <param name="model"></param> private void ValidateSavePermission(UserModel model) { if (model.BusinessApplicationId == Guid.Empty) model.ErrorList.Add(Resources.Administration.BusinessApplicationNotSelected); if (model.SelectedUserType == 3 && model.ClientId == null) model.ErrorList.Add(Resources.Administration.ClientNotSelected); //validation for selected roles else if (model.SelectedUserType == 4 && string.IsNullOrEmpty(model.CheckedRoles)) model.ErrorList.Add(Resources.Administration.RolesNotSelected); }
public ActionResult SaveUser(UserModel model) { ValidateUserSave(model); if (!model.HasErrors) { if (model.OpenMode == (int)ScreenOpenMode.Add) { MembershipCreateStatus status = AuthorizationBusiness.Instance.SaveUser(model); if (status != MembershipCreateStatus.Success) model.ErrorList.Add(Resources.Administration.UserCreationError); if (model.SelectedUserType != 1) AuthorizationBusiness.VerifySaveVestalisUserApplications(model, UserName); if (model.SelectedUserType == 4) AuthorizationBusiness.SaveRolesForCotecnaUser(model); } else if (model.OpenMode == (int)ScreenOpenMode.Edit) { AuthorizationBusiness.Instance.EditUser(model); } } Session.Add("UserModel", model); return Json(model); }
/// <summary> /// Save the permission in the system /// </summary> /// <param name="selectedRoles">List of chosen roles</param> /// <param name="clientId">Id of selected client</param> /// <param name="businessAppId">If of selected business application</param> /// <param name="businessAppEdit">if of business application to edit</param> /// <param name="userType">User type</param> /// <param name="userName">E-mail of the user</param> /// <param name="openMode">Open mode</param> /// <returns>Json result</returns> public ActionResult SavePermission(string selectedRoles, Guid? clientId, Guid? businessAppId, Guid? businessAppEdit, int? userType, string userName, int? openMode) { UserModel model = new UserModel() { BusinessApplicationId = businessAppId.GetValueOrDefault(), BusinessApplicationIdPer = businessAppEdit.GetValueOrDefault(), CheckedRoles = selectedRoles, ClientId = clientId, SelectedUserType = userType.GetValueOrDefault(), Email = userName, OpenMode = openMode.GetValueOrDefault() }; ValidateSavePermission(model); if (!model.HasErrors) { if (userType.GetValueOrDefault() == 3) { AuthorizationBusiness.VerifySaveVestalisUserApplications(model, UserName); } else if (userType.GetValueOrDefault() == 4) { AuthorizationBusiness.VerifySaveVestalisUserApplications(model, UserName); if (!model.HasErrors) AuthorizationBusiness.SaveRolesForCotecnaUser(model); } } return Json(model); }
public ActionResult NewUser() { UserModel model = new UserModel(); model.OpenMode = (int)ScreenOpenMode.Add; model.UserTypes = UserTypes; model.BusinessApplications = RetrieveBusinessApplications(); Session.Add("UserModel", model); return View("User", model); }
/// <summary> /// Open the screen User in Edit modes /// </summary> /// <param name="userNameEdit">User for editing</param> /// <returns></returns> public ActionResult EditUser(string userNameEdit) { int userType = int.Parse(AuthorizationBusiness.GetUserParameter(userNameEdit, "UserType")); UserModel model = new UserModel(); model.UserTypes = UserTypes; model.OpenMode = (int)ScreenOpenMode.Edit; model.SelectedUserType = userType; MembershipUser user = Membership.GetUser(userNameEdit); model.Email = user.UserName; model.BusinessApplications = RetrieveBusinessApplications(); int pageSize = Cotecna.Vestalis.Web.Properties.Settings.Default.PageSize; ParameterSearchPermission parameters = new ParameterSearchPermission() { PageSize = pageSize, SelectedPage = 1, SortedColumn = "BusinessApplication", SortDirection = SortDirection.Ascending, LoginName = userNameEdit, IsGlobalAdmin = IsGlobalAdmin, LoggedUserName = UserName }; model.PermissionList = AuthorizationBusiness.GetPermissionListByUser(parameters); Session.Add("UserModel", model); return View("User", model); }