Ejemplo n.º 1
0
        public string Update(UserViewModel model)
        {
            var user = new ApplicationUser
            {
                UserName          = model.UserName.ToLower(),
                FirstName         = model.FirstName,
                LastName          = model.LastName,
                Email             = model.Email,
                Role              = model.Role,
                Position          = model.Position,
                Org               = model.Org,
                GeoCode           = model.GeoCode,
                ReceiveEmailAlert = model.ReceiveEmailAlert
            };
            string message = string.Empty;

            if (model.Action == Constant.UserAdmin.Operation.Add)
            {
                var storedUser = _userAdminRepository.GetActiveUser(user.UserName);
                if (storedUser != null && string.IsNullOrEmpty(storedUser.UserName))
                {
                    return("An user already exist with the same Username ('" + user.UserName + "').");
                }
                storedUser = _userAdminRepository.GetUserByEmail(user.Email);
                if (storedUser != null && string.IsNullOrEmpty(storedUser.UserName))
                {
                    return("An user already exist with the same Email ('" + user.Email + "').");
                }
                var encryptedPassword = user.PasswordHash = _userManager.PasswordHasher.HashPassword("123456");
                user.PasswordHash = encryptedPassword;
                message           = _userAdminRepository.AddUser("", user);
                SendUserAddedMail(user, Request.Url.AbsoluteUri.Replace("UserOperation/Update", string.Empty));
            }
            else if (model.Action == Constant.UserAdmin.Operation.Edit)
            {
                var storedUser = _userAdminRepository.GetActiveUser(user.UserName);
                if (storedUser == null)
                {
                    return(user.UserName + " User Does Not Exist");
                }
                user.FirstName         = model.FirstName;
                user.LastName          = model.LastName;
                user.Role              = model.Role;
                user.Position          = model.Position;
                user.GeoCode           = model.GeoCode;
                user.Org               = model.Org;
                user.ReceiveEmailAlert = model.ReceiveEmailAlert;
                user.IsActive          = storedUser.IsActive;
                message = _userAdminRepository.EditUser(storedUser.Id, user);
                SendUserEditedMail(user, Request.Url.AbsoluteUri.Replace("UserOperation/Update", string.Empty));
            }
            return(message);
        }