Пример #1
0
        public async Task <IActionResult> RemoveRoleFromUser(string userId, string roleName)
        {
            AppUser userFound = await _userManager.FindByIdAsync(userId);

            if (userFound == null)
            {
                return(RedirectToAction(nameof(UserList)));
            }

            var result = await _userManager.RemoveFromRoleAsync(userFound, roleName);

            if (result.Succeeded)
            {
                return(RedirectToAction("RolesManagement", new { id = userId }));
            }

            IList <string> userRoles = await _userManager.GetRolesAsync(userFound);

            List <IdentityRole> identityRoles = _roleManager.Roles.ToList();

            RolesManagementViewModel viewModel = new RolesManagementViewModel(userId, userRoles, identityRoles);

            ViewBag.ErrorMsg = "Failed to change role for user!";

            return(View("RolesManagment", viewModel));
        }
        public async Task <IActionResult> RolesManagement(string id)
        {
            ClassUser userFound = await _userManager.FindByIdAsync(id);

            if (userFound == null)
            {
                return(RedirectToAction(nameof(UserList)));
            }

            IList <string> userRoles = await _userManager.GetRolesAsync(userFound);

            List <IdentityRole> identityRoles = _roleManager.Roles.ToList();

            RolesManagementViewModel viewModel = new RolesManagementViewModel(id, userRoles, identityRoles);

            return(View(viewModel));
        }
Пример #3
0
        public async Task <IActionResult> RolesManagement(string id)
        {
            //Hämta anvädaren
            AppUser userFound = await _userManager.FindByIdAsync(id);

            if (userFound == null)
            {
                return(RedirectToAction(nameof(UserList)));
            }

            // Hämta anvädarens roller. (IList behövs för att det är ett await anrop!)
            IList <string> roles = await _userManager.GetRolesAsync(userFound);

            //Hämta tillgängliga roller.
            List <IdentityRole> identityRoles = _roleManager.Roles.ToList();

            RolesManagementViewModel viewModel = new RolesManagementViewModel(id, roles, identityRoles);

            return(View(viewModel));
        }
Пример #4
0
        public async Task <ActionResult> Index()
        {
            RolesManagementViewModel model = new RolesManagementViewModel();

            if (null != this.User && this.User.Identity.IsAuthenticated)
            {
                var UserManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
                var user        = await UserManager.FindByNameAsync(this.User.Identity.Name);

                if (null != user)
                {
                    model.Profile = InsuranceBusiness.BusinessLayer.GetUserProfile(user.Id);
                }
                else
                {
                    return(RedirectToAction("LogOff", "Account"));
                }
            }
            else
            {
                return(RedirectToAction("Login", "Account"));
            }

            try
            {
                //var grid = MVCGridDefinitionTable.GetDefinition<UserProfileModelObject>("RolesManagementGrid");
            }
            catch (Exception)
            {
                MVCGridDefinitionTable.Add("RolesManagementGrid", new MVCGridBuilder <UserProfileModelObject>()
                                           .WithAuthorizationType(AuthorizationType.AllowAnonymous)
                                           .AddColumns(cols =>
                {
                    // Add your columns here
                    cols.Add().WithColumnName("Username")
                    .WithHeaderText(Resources.Resources.Username)
                    .WithValueExpression(i => i.User.UserName)
                    .WithAllowChangeVisibility(true)
                    .WithFiltering(true)
                    .WithSorting(true)
                    .WithVisibility(true, true);             // use the Value Expression to return the cell text for this column
                    cols.Add().WithColumnName("FirstName")
                    .WithHeaderText(Resources.Resources.Name)
                    .WithValueExpression(i => i.FirstName)
                    .WithAllowChangeVisibility(true)
                    .WithFiltering(true)
                    .WithSorting(true)
                    .WithVisibility(true, true);             // use the Value Expression to return the cell text for this column
                    cols.Add("CreateDate")
                    .WithHeaderText(Resources.Resources.RegisterDate)
                    .WithCellCssClassExpression(p => "dateCell")
                    .WithValueExpression(p => p.CreateDate.ToString("dd-MM-yyyy"));
                    cols.Add("Activate")
                    .WithHtmlEncoding(false)
                    .WithSorting(false)
                    .WithHeaderText(" ")
                    .WithCellCssClassExpression(p => "controlCell")
                    //.WithPlainTextValueExpression((p, c) => p.User.EmailConfirmed ? "deactivate" : "activate")
                    .WithValueExpression((p, c) => c.UrlHelper.Action(p.User.EmailConfirmed ? "Deactivate" : "Activate", "UsersManagement", new { id = p.ID }))
                    .WithValueTemplate("<a href='{Value}' class='' role='button' style='margin-right:5px;color:limegreen;'>" + MVCGridConfig.GetActivateCommandCode("{Model.User.EmailConfirmed}") + "</a>" + "<a href='UsersManagement/BlockUser/{Model.ID}' class='' role='button' style='margin-right:5px;color:red'>" + MVCGridConfig.GetBlockCommandCode("{Model.User.EmailConfirmed}") + "</a>");
                })
                                           .WithAdditionalQueryOptionNames("Search")
                                           .WithSorting(true, "FirstName")
                                           .WithPaging(paging: true, itemsPerPage: 10, allowChangePageSize: true, maxItemsPerPage: 100)
                                           .WithRetrieveDataMethod((context) =>
                {
                    // Query your data here. Obey Ordering, paging and filtering parameters given in the context.QueryOptions.
                    // Use Entity Framework, a module from your IoC Container, or any other method.
                    // Return QueryResult object containing IEnumerable<YouModelItem>

                    var options = context.QueryOptions;
                    var result  = new QueryResult <UserProfileModelObject>();
                    var query   = BusinessItemsLists.GetUsers();
                    if (!String.IsNullOrWhiteSpace(options.SortColumnName))
                    {
                        switch (options.SortColumnName.ToLower())
                        {
                        case "firstname":
                            if (options.SortDirection == SortDirection.Asc ||
                                options.SortDirection == SortDirection.Unspecified)
                            {
                                query = query.OrderBy(p => p.FirstName).ToList();
                            }
                            else
                            {
                                query = query.OrderByDescending(p => p.FirstName).ToList();
                            }

                            break;

                        case "lastname":
                            //query = query.OrderBy(p => p.LastName, options.SortDirection);
                            break;
                        }
                    }

                    string globalSearch = options.GetAdditionalQueryOptionString("Search");
                    if (null != globalSearch)
                    {
                        query = query.Where(i =>
                                            i.FirstName.ToLower().Contains(globalSearch.ToLower()) ||
                                            i.LastName.ToLower().Contains(globalSearch.ToLower()) ||
                                            i.User.UserName.ToLower().Contains(globalSearch.ToLower()) ||
                                            i.ContactEmail.ToLower().Contains(globalSearch.ToLower())
                                            ).ToList();
                    }

                    result.TotalRecords = query.Count();

                    if (options.GetLimitOffset().HasValue)
                    {
                        query = query
                                .Skip(options.GetLimitOffset().Value)
                                .Take(options.GetLimitRowcount().Value)
                                .ToList();
                    }

                    result.Items = query;

                    return(result);
                })
                                           );
            }

            return(View(model));
        }