public MembershipUser CreateUser(string email, string name, string password)
        {
            MembershipUser membershipUser = GetUser(name, false);

            if (membershipUser != null)
            {
                return(null);
            }

            var user = new BllUser
            {
                Email    = email,
                UserName = name,
                Password = Crypto.HashPassword(password)
            };

            UserService.Create(user);
            var userRole = RoleService.GetOneByPredicate(r => r.Name == "User");

            if (userRole != null)
            {
                RoleService.AddRoleToUser(UserService.GetOneByPredicate(u => u.UserName == name).Id, userRole.Id);
            }
            membershipUser = GetUser(name, false);
            return(membershipUser);
        }
Beispiel #2
0
        public ActionResult UpdateUserRole(int userId, string roleName)
        {
            var user = userService.GetUserById(userId).ToMvcUser(roleService);

            if (user == null)
            {
                return(RedirectToAction("NotFound", "Error"));
            }

            if (user.Login == User.Identity.Name)
            {
                return(RedirectToAction("BadRequest", "Error"));
            }

            var role = roleService.GetAllRoles().FirstOrDefault(r => r.Name == roleName);

            if (role == null)
            {
                return(RedirectToAction("BadRequest", "Error"));
            }

            if (user.Roles.Contains(roleName))
            {
                roleService.RemoveRoleFromUser(userId, role.Id);
            }
            else
            {
                roleService.AddRoleToUser(userId, role.Id);
            }

            return(PartialView("_UpdateUserRole", user));
        }
Beispiel #3
0
        public MembershipUser CreateUser(string email, string login, string password)
        {
            MembershipUser membershipUser = GetUser(login, false);

            if (membershipUser != null)
            {
                return(null);
            }

            UserEntity user = new UserEntity
            {
                Email    = email,
                Login    = login,
                Password = Crypto.HashPassword(password)
            };

            UserService.CreateUser(user);
            RoleEntity userRole = RoleService.GetAllRoles().SingleOrDefault(r => r.Name == "user");

            if (userRole != null)
            {
                RoleService.AddRoleToUser(UserService.GetUserByLogin(user.Login).Id,
                                          userRole.Id);
            }
            membershipUser = GetUser(login, false);
            return(membershipUser);
        }
Beispiel #4
0
 public void AddRoleToUser_addRoleToUserIfUserNotContainsRole()
 {
     var user = Builder<User>.CreateNew().Build();
     var role = Builder<Role>.CreateNew().Build();
     _userRepository = Mockery.Stub<IUserRepository>();
     _roleRepository = Mockery.Stub<IRoleRepository>();
     _roleService = new RoleService(_roleRepository, _userRepository);
     _roleService.AddRoleToUser(user, role);
     Assert.AreEqual(true, user.Roles.Contains(role));
 }
Beispiel #5
0
 public void AddRoleToUser_notAddRoleToUserIfUserContainsRole()
 {
     var role = Builder<Role>.CreateNew().Build();
     var user = Builder<User>.CreateNew().With(x => x.Roles = new List<Role> { role }).Build();
     _userRepository = Mockery.Stub<IUserRepository>();
     _roleRepository = Mockery.Stub<IRoleRepository>();
     _roleService = new RoleService(_roleRepository, _userRepository);
     _roleService.AddRoleToUser(user, role);
     Assert.AreEqual(true, user.Roles.Contains(role));
     user.Roles.Remove(role);
     Assert.AreEqual(false, user.Roles.Contains(role));
 }
        public ActionResult UpdateUserRole(string userName, string roleName)
        {
            var user   = userService.GetOneByPredicate(u => u.UserName == userName).ToMvcUser(roleService);
            int userId = user.Id;

            if (user.Roles.Contains(roleName))
            {
                roleService.RemoveRoleFromUser(userId, roleService.GetAll().FirstOrDefault(r => r.Name == roleName).Id);
            }
            else
            {
                roleService.AddRoleToUser(userId, roleService.GetAll().FirstOrDefault(r => r.Name == roleName).Id);
            }

            if (Request.IsAjaxRequest())
            {
                return(PartialView(user));
            }
            return(View(user));
        }