Beispiel #1
0
        public void SaveUser(UserEditModel model)
        {
            try
            {
                SetStaticToModel(model,false);

                var user = new User();
                if (model.Id > 0)
                    user = UserDao.Load(model.Id);
                if (!ValidateModel(model, user))
                {
                    return;
                }
                user.IsActive = model.IsActive;
                string oldPassword = user.Password;
                if (model.Id == 0)
                {
                    user.Email = model.Email;
                    user.Login = model.Login;
                    user.Name = model.UserName;
                    user.Password = model.Password;
                    user.IsNew = true;
                    if (IsUserFrom1C((UserRole)(model.RoleId)))
                        user.IsFirstTimeLogin = true;
                    user.Code = string.Empty;
                    if ((model.RoleId & (int) UserRole.Employee) > 0)
                    {
                        if (model.ManagerId != 0)
                            user.Manager = UserDao.Load(model.ManagerId);
                        /*if (model.PersonnelId != 0)
                            user.PersonnelManager = UserDao.Load(model.PersonnelId);*/
                    }
                }
                else
                {
                    if (!model.IsPasswordHide)
                    {
                        user.Email = model.Email;
                        user.Password = model.Password;
                        if (!IsUserFrom1C((UserRole) (model.RoleId)) || user.IsNew)
                        {
                            user.Login = model.Login;
                            user.Name = model.UserName;
                        }
                    }
                }
                user = UserDao.MergeAndFlush(user);
                model.Id = user.Id;
                model.IsNew = user.IsNew;
                model.Version = user.Version;
                model.UserNameStatic = model.UserName;
                if (((user.RoleId & (int) UserRole.Employee) == 0) &&
                    (model.ManagerId != 0))
                    model.ClearManagers = true;

                SetControlStates(model,user);
                if (!string.IsNullOrEmpty(user.Email) && (oldPassword != user.Password))
                {
                    SendEmail(model,
                              user.Email,
                              "Изменение пароля",
                              string.Format("Ваш пароль был изменен.Новый пароль {0}", user.Password)
                        );
                    if (!string.IsNullOrEmpty(model.EmailDto.Error))
                    {
                        model.Error =
                            "Данные пользователя изменены успешно, однако письсмо с новым паролем не было отправлено. Ошибка: " +
                            model.EmailDto.Error;
                    }
                }

            }
            catch (Exception ex)
            {
                Log.Error("Exception", ex);
                model.Error = string.Format("Исключение {0} ", ex.GetBaseException().Message);
                model.NeedToReload = true;
            }
        }
Beispiel #2
0
        public void SetStaticToModel(UserEditModel model,bool setStatic)
        {
            model.Role = GetUserRoleName(model.RoleId);//GetRoleList(false,role);
            model.Managers = GetUsersWithRoleList(UserRole.Manager, true);

            SetStaticUserPopertiesToModel(model);
        }
Beispiel #3
0
 protected bool ValidateModel(UserEditModel model, User user)
 {
     if (UserDao.IsLoginWithOtherIdExists(model.Login, model.Id))
     {
         model.Error = "Логин должен быть уникальным";
         return false;
     }
     if ((model.RoleId & (int)UserRole.Employee) > 0)
     {
         if (model.ManagerId == 0)
         {
             model.Error = "Необходимо указать руководителя для сотрудника.";
             return false;
         }
         /*if (model.PersonnelId == 0)
         {
             model.Error = "Необходимо указать кадровика для сотрудника.";
             return false;
         }*/
     }
     return true;
 }
Beispiel #4
0
        public void GetUserEditModel(UserEditModel model)
        {
            UserRole role = CurrentUser.UserRole;
            if ((role & UserRole.Admin) != UserRole.Admin && (role & UserRole.PersonnelManager) != UserRole.PersonnelManager)
                throw new ArgumentException("Доступ запрещен.");

            model.Managers = GetUsersWithRoleList(UserRole.Manager, true);

            if (model.Id > 0)
            {
                User user = UserDao.Load(model.Id);
                model.Code = user.Code;
                model.Email = user.Email;
                model.IsActive = user.IsActive;
                model.IsNew = user.IsNew;
                model.Login = user.Login;
                model.Password = user.Password;
                model.RoleId = user.RoleId;
                model.Role = GetUserRoleName(user.RoleId);
                model.UserName = user.FullName;
                model.UserNameStatic = user.FullName;
                model.Version = user.Version;
                if (user.Personnels.Count() > 0)
                    model.PersonnelName = user.Personnels.Aggregate(string.Empty, (current, entity) => current + (entity.FullName + "; "));
                if ((user.RoleId & (int)UserRole.Employee) > 0)
                {
                    if (user.Manager != null)
                        model.ManagerId = user.Manager.Id;
                }
                SetControlStates(model, user);
            }
            else
            {
                model.IsActive = true;
                model.IsNew = true;
                SetControlStates(model,null);
            }
        }
Beispiel #5
0
 protected void SetStaticUserPopertiesToModel(UserEditModel model)
 {
     if (model.Id > 0)
     {
         User user = UserDao.Load(model.Id);
         model.UserNameStatic = user.FullName;
         if(user.Personnels.Count() > 0)
             model.PersonnelName = user.Personnels.Aggregate(string.Empty, (current, entity) => current + (entity.FullName + "; "));
     }
 }
Beispiel #6
0
        protected void SetControlStates(UserEditModel model, User user)
        {
            model.IsActiveEditable = true;
            model.IsRoleEditable = model.Id == 0;
            model.IsManagerEditable = model.Id == 0;
            model.IsPersonnelEditable = model.Id == 0;
            if (model.Id == 0)
            {
                model.IsLoginEditable = true;
                model.IsUserNameEditable = true;
            }
            else
            {
                if ((CurrentUser.UserRole & UserRole.PersonnelManager) == UserRole.PersonnelManager && !user.IsFirstTimeLogin)
                {
                    model.IsPasswordHide = true;
                    model.Password = "******";
                }
                bool isEditable = !IsUserFrom1C((UserRole)(model.RoleId)) || model.IsNew && !model.IsPasswordHide;
                model.IsLoginEditable = isEditable ;
                model.IsUserNameEditable = isEditable;

            }
        }
Beispiel #7
0
 protected bool ValidateModel(UserEditModel model)
 {
     CheckUserRole(true);
     return ModelState.IsValid;
 }
Beispiel #8
0
        public ActionResult UserEdit(UserEditModel model)
        {
            if (!ValidateModel(model))
            {
                AdminBl.SetStaticToModel(model,true);
                return View(model);
            }
            AdminBl.SaveUser(model);
            if(model.NeedToReload)
            {
                ModelState.Clear();
                ModelState.AddModelError("Error", model.Error);
                UserEditModel newModel = new UserEditModel { Id = model.Id};
                AdminBl.GetUserEditModel(newModel);
                return View(newModel);
            }
            if(!string.IsNullOrEmpty(model.Error))
                ModelState.AddModelError("Error", model.Error);

            if(model.ClearManagers)
            {
                if (ModelState.ContainsKey("ManagerId"))
                    ModelState.Remove("ManagerId");
                model.ManagerId = 0;
                /*if (ModelState.ContainsKey("PersonnelId"))
                    ModelState.Remove("PersonnelId");
                model.PersonnelId = 0;*/
            }
            return View(model);
        }
Beispiel #9
0
 public ActionResult UserEdit(int id)
 {
     CheckUserRole(true);
     UserEditModel model = new UserEditModel{Id = id};
     AdminBl.GetUserEditModel(model);
     return View(model);
 }