示例#1
0
        public ActionResult Add(User user)
        {
            user.Role = repo.GetRole(user.RoleId);
            if (ModelState.IsValid)
            {
                try
                {
                    MembershipService.CreateUser(user.Username, user.RealName, user.Password, user.Password, user.Role.Name);

                    MudHookNotifications.Set(new Notification("success", "User was successfully added."));

                    return RedirectToAction("Index", "Users");
                }
                catch (ArgumentException ae)
                {
                    MudHookNotifications.Set(new Notification("error", ae.Message));
                }
            }

            return View(user);
        }
示例#2
0
        public void CreateUser(string userName, string realName, string password, string email, string roleName)
        {
            Role role = GetRole(roleName);

            if (string.IsNullOrEmpty(userName.Trim()))
                throw new ArgumentException("Please enter a username");
            if (string.IsNullOrEmpty(realName.Trim()))
                throw new ArgumentException("Please enter a display name");
            if (string.IsNullOrEmpty(password.Trim()))
                throw new ArgumentException("Please enter a password");
            if (string.IsNullOrEmpty(email.Trim()))
                throw new ArgumentException("Please enter a valid email address");
            if (!RoleExists(role))
                throw new ArgumentException("The role selected for this user does not exist! Contact an administrator!");
            if (this.db.Users.Any(user => user.Username == userName))
                throw new ArgumentException("Username is already being used");

            User newUser = new User()
            {
                Username = userName,
                RealName = realName,
                Password = MudHookSecurity.Hash(password, userName),
                Email = email,
                Bio = "",
                Status = UserStatus.active,
                RoleId = role.Id
            };

            try
            {
                AddUser(newUser);
            }
            catch (ArgumentException ae)
            {
                throw ae;
            }
            catch (Exception e)
            {
                throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
                    "If the problem persists, please contact your system administrator.");
            }

            Save();
        }
示例#3
0
 public void AddUser(User user)
 {
     db.Users.Add(user);
 }
示例#4
0
        public bool UserExists(User user)
        {
            if (user == null)
                return false;

            return (db.Users.SingleOrDefault(u => u.Id == user.Id || u.Username == user.Username) != null);
        }
示例#5
0
        public Role GetRoleForUser(User user)
        {
            if (!UserExists(user))
                throw new ArgumentException(MissingUser);

            return GetRole(user.RoleId);
        }
示例#6
0
 public void EditUser(User user)
 {
     db.Entry(user).State = EntityState.Modified;
     Save();
 }
示例#7
0
 public ActionResult Edit(User user)
 {
     if (ModelState.IsValid)
     {
         if (string.IsNullOrEmpty(user.Password))
         {
             user.Password = repo.GetUser(user.Id).Password;
         }
         else
         {
             user.Password =  Convert.ToBase64String(
                 MudHookSecurity.GenerateSaltedHash(Encoding.UTF8.GetBytes(user.Password), Encoding.UTF8.GetBytes(user.Username.ToLower())));
         }
         repo.EditUser(user);
         MudHookNotifications.Set(new Notification("success", "User has been updated"));
         RedirectToAction("Index");
     }
     else
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         string message = "";
         foreach (var e in allErrors)
         {
             message += e.ErrorMessage + ",";
         }
         MudHookNotifications.Set(new Notification("error", message.TrimEnd()));
     }
     return View(user);
 }