Пример #1
0
        public SaveUserResult Save(UserViewModel viewModel, UserSessionObject currentUser)
        {
            var result = new SaveUserResult();

            if (viewModel.IsNew)
            {
                viewModel.UserPassword = ConfigurationManager.AppSettings["DefaultPassword"];
            }

            UserDomainModelBuilder builder = BuilderResolverService.Get <UserDomainModelBuilder, UserViewModel>(viewModel);

            Constructor.ConstructDomainModelData(builder);
            UserData domainModel = builder.GetDataModel();

            if (viewModel.Id == 0)
            {
                domainModel.RefUserCreatedBy = currentUser.UserId;
            }

            int id = UserRepository.SaveData(domainModel);

            if (id != 0)
            {
                result = new SaveUserResult(id, domainModel.FullFirstAndLastName);
            }

            return(result);
        }
Пример #2
0
        public JsonResult Save(UserViewModel viewModel)
        {
            var currentUser = Session.GetUser();

            JsonResult         response   = (JsonResult)RouteData.Values["validation"];
            ValidationResponse validation = (ValidationResponse)response.Data;

            if (validation.Success)
            {
                SaveUserResult result = UserService.Save(viewModel, currentUser);
                if (result.Success)
                {
                    response.Data = result;
                    if (currentUser.UserId == viewModel.Id)
                    {
                        AccessControlService.SetCurrentUser(Session, currentUser.UserId);
                    }
                }
                else
                {
                    response = Json(result);
                }
            }

            return(response);
        }
Пример #3
0
        public ActionResult ChangePassword(PasswordChangeViewModel viewModel)
        {
            var            currentUser   = Session.GetUser();
            UserViewModel  userViewModel = UserService.Get(currentUser.UserId);
            SaveUserResult saveResult    = null;
            ActionResult   result        = null;

            if (viewModel.NewPassword == viewModel.RepeatPassword &&
                viewModel.OldPassword != viewModel.NewPassword &&
                UserService.IsPasswordValid(viewModel.OldPassword, userViewModel.UserPassword))
            {
                userViewModel.UserPassword = UserService.HashPassword(viewModel.NewPassword);
                saveResult = UserService.Save(userViewModel, currentUser);

                if (saveResult.Success)
                {
                    if (Request.Cookies[SessionConstant.USER_ID] != null)
                    {
                        Response.Cookies[SessionConstant.USER_ID].Expires = DateTime.Now.AddDays(-1);
                    }
                    Session.Remove(SessionConstant.USER);
                    Session.Remove(SessionConstant.USER_ID);
                    Session.Remove(SessionConstant.USERNAME);
                }

                result = Json(saveResult);
            }
            else
            {
                saveResult         = new SaveUserResult();
                saveResult.Message = "Data invalid!";
                result             = Json(saveResult);
            }

            return(result);
        }
        public static async Task <SaveUserResult> SaveUserAsync(SiteUserService _siteUserService, UserService _userService, ApplicationUser creatorUserDb, SaveSiteUserInput saveSiteUser)
        {
            var result = new SaveUserResult();

            var             siteId          = saveSiteUser.SiteId;
            var             mail            = saveSiteUser.Mail.ToLower().Trim();
            SiteUserDbModel siteUserDbModel = null;

            var siteUserId = saveSiteUser.SiteUserId;

            if (String.IsNullOrEmpty(siteUserId))
            {
                // 1: Creation
                siteUserDbModel = await _siteUserService.FindByEmailAsync(siteId, mail);

                if (siteUserDbModel != null)
                {
                    result.Error      = "user_email_already_added";
                    result.IsSuccess  = false;
                    result.SiteUserId = siteUserDbModel.Id;
                    return(result);
                }
                siteUserDbModel        = new SiteUserDbModel();
                siteUserDbModel.SiteId = siteId;
            }
            else
            {
                // 1: Mise à jour
                siteUserDbModel = await _siteUserService.FindAsync(siteUserId);

                var siteUserDbModelCheck = await _siteUserService.FindByEmailAsync(siteId, mail);

                if (siteUserDbModelCheck != null && siteUserDbModel.Id != siteUserDbModelCheck.Id)
                {
                    result.Error      = "user_email_already_added";
                    result.IsSuccess  = false;
                    result.SiteUserId = siteUserDbModel.Id;
                    return(result);
                }
            }

            // On doit créer le userSite
            siteUserDbModel.Mail         = mail;
            siteUserDbModel.FirstName    = saveSiteUser.FirstName;
            siteUserDbModel.LastName     = saveSiteUser.LastName;
            siteUserDbModel.Birthdate    = saveSiteUser.Birthdate;
            siteUserDbModel.Tags         = saveSiteUser.Tags;
            siteUserDbModel.Comments     = saveSiteUser.Comments;
            siteUserDbModel.Civility     = saveSiteUser.Civility;
            siteUserDbModel.IsEmailNotif = saveSiteUser.IsEmailNotif;
            siteUserDbModel.FlaggedRoles = saveSiteUser.Roles;

            var updatedUserDb = await _userService.FindApplicationUserByEmailAsync(mail);

            if (updatedUserDb != null)
            {
                siteUserDbModel.UserId = updatedUserDb.Id;
            }

            await _siteUserService.SaveAsync(siteUserDbModel);

            result.SiteUserId = siteUserDbModel.Id;

            var resultManageRoleAdministrator = await ManageRolesAsync(_userService, saveSiteUser, updatedUserDb, siteId, SiteUserRole.Administrator);

            switch (resultManageRoleAdministrator)
            {
            case "user_updated":
                result.SendMailAdmin = new SendMailAdmin()
                {
                    Mail        = mail,
                    UserCreated = updatedUserDb,
                    SiteId      = siteId,
                    UserCreator = creatorUserDb
                };
                break;

            case "no_user_found":
                result.SendMailAdmin = new SendMailAdmin()
                {
                    Mail        = mail,
                    SiteId      = siteId,
                    UserCreator = creatorUserDb
                };
                result.Error = "no_user_found";
                break;
            }

            var resultManageRoleUserPrivate = await ManageRolesAsync(_userService, saveSiteUser, updatedUserDb, siteId + "_private_user", SiteUserRole.PrivateUser);

            return(result);
        }