public IHttpActionResult AddAndRemoveRoles(List <string> userid)
 {
     try
     {
         _rolemanager.AddUserToRole(userid[0], "user");
         _rolemanager.RemoveUserFromRole(userid[0], "pending");
         return(Ok());
     }
     catch
     {
         return(BadRequest());
     }
 }
Пример #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            string imgPath = @"Images\avatar.jpg";

            if (model.Image != null)
            {
                var helper = new UserRepository();
                try
                {
                    imgPath = helper.SaveImage(model.Image);
                }
                catch
                {
                    ModelState.AddModelError("", "Det måste vara en bild");
                }
            }
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName    = model.Email,
                    Email       = model.Email,
                    Image       = imgPath,
                    Firstname   = model.Firstname,
                    Lastname    = model.Lastname,
                    Phone       = model.Phone,
                    Title       = model.Title,
                    Description = null
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var eN = new EmailNotification();
                    eN.SendRegisterEmail(user.Email);
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    var roleManager = new RoleRepository();
                    roleManager.AddUserToRole(user.Id, "pending");

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("WaitForVerification", "Home"));
                }
                AddErrors(result);
            }
            if (imgPath != null)
            {
                UserRepository.DeleteImg(imgPath);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #3
0
 /// <summary>
 /// Adds the specified user names to the specified roles
 /// </summary>
 /// <param name="users"></param>
 /// <param name="roles"></param>
 public void AddUsersToRoles(Collection <AppUser> users, Collection <Role> roles)
 {
     foreach (AppUser u in users)
     {
         foreach (Role r in roles)
         {
             _roleRepository.AddUserToRole(u, r);
         }
     }
     _roleRepository.Save();
 }
Пример #4
0
        private async Task <bool> ChangeUserRole(string userName, string newRole)
        {
            ApplicationUser user = await UserManager.FindByNameAsync(userName);

            string oldRole = _roleRepository.GetUserRole(user.Id);

            if (oldRole != newRole)
            {
                _roleRepository.RemoveUserFromRole(user.Id, oldRole);
                _roleRepository.AddUserToRole(user.Id, newRole);

                return(true);
            }

            return(false);
        }
Пример #5
0
 public void AddUserToRole(string userId, string roleId)
 {
     _roleRepository.AddUserToRole(userId, roleId);
 }