public async Task <IActionResult> Create(CreateRoleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Clients = await ConfigurationDbContext.Clients.AsNoTracking().ToListAsync();

                return(View(model));
            }

            if (ApplicationRoleExists(model.Name))
            {
                model.Clients = await ConfigurationDbContext.Clients.AsNoTracking().ToListAsync();

                ModelState.AddModelError("", "Role with same name exist!");
                return(View(model));
            }

            var applicationRole = new GearRole
            {
                Name      = model.Name,
                Title     = model.Title,
                IsDeleted = model.IsDeleted,
                ClientId  = model.ClientId,
            };
            var result = await _roleManager.CreateAsync(applicationRole);

            var user = await _signInManager.UserManager.GetUserAsync(User);

            var client = ConfigurationDbContext.Clients.AsNoTracking().FirstOrDefault(x => x.Id.Equals(model.ClientId))
                         ?.ClientName;
            await _notify.SendNotificationAsync(new Notification
            {
                Content            = $"{user?.UserName} created the role {applicationRole.Name} for {client}",
                Subject            = "Info",
                NotificationTypeId = NotificationType.Info
            });

            if (!result.Succeeded)
            {
                model.Clients = await ConfigurationDbContext.Clients.AsNoTracking().ToListAsync();

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            else
            {
                var role = await _applicationDbContext.Roles.AsNoTracking().SingleOrDefaultAsync(m => m.Name == model.Name);

                if (role == null)
                {
                    return(RedirectToAction(nameof(Index)));
                }

                if (model.SelectedPermissionId.Any())
                {
                    var listOfRolePermission = new List <RolePermission>();
                    foreach (var _ in model.SelectedPermissionId)
                    {
                        var permission = await _permissionsContext.Permissions.AsNoTracking()
                                         .SingleOrDefaultAsync(x => x.Id == Guid.Parse(_));

                        if (permission != null)
                        {
                            listOfRolePermission.Add(new RolePermission
                            {
                                PermissionId = permission.Id,
                                RoleId       = role.Id
                            });
                        }
                    }

                    await _permissionsContext.RolePermissions.AddRangeAsync(listOfRolePermission);
                }

                try
                {
                    await _permissionsContext.SaveChangesAsync();

                    await _permissionService.RefreshCacheByRoleAsync(applicationRole.Name);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Exemple #2
0
 /// <summary>
 /// Is role subscribed
 /// </summary>
 /// <param name="role"></param>
 /// <returns></returns>
 public bool IsRoleSubscribed(GearRole role)
 {
     return(SubscribedRoles?.Select(x => x.Name).Contains(role?.Name) ?? false);
 }
Exemple #3
0
        public async Task <JsonResult> CreateRolePartialView(CreateRoleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { success = false, message = "Model is not valid" }));
            }

            var applicationRole = new GearRole
            {
                Name      = model.Name,
                Title     = model.Title,
                IsDeleted = model.IsDeleted,
                Created   = DateTime.Now,
                Author    = User.Identity.Name,
                ClientId  = model.ClientId,
            };
            var result = await _roleManager.CreateAsync(applicationRole);

            if (!result.Succeeded)
            {
                return(Json(new { success = false, message = "Error on create role" }));
            }


            var roleId = await _applicationDbContext.Roles.AsNoTracking()
                         .SingleOrDefaultAsync(x => x.Name == model.Name);

            if (roleId == null)
            {
                return(Json(new { success = false, message = "Role not found!!!" }));
            }

            if (model.SelectedPermissionId.Any())
            {
                foreach (var _ in model.SelectedPermissionId)
                {
                    var permission =
                        await _applicationDbContext.Permissions.SingleOrDefaultAsync(x => x.Id == Guid.Parse(_));

                    if (permission != null)
                    {
                        var newRolePermission = new RolePermission
                        {
                            RoleId       = roleId.Id,
                            PermissionId = permission.Id
                        };
                        try
                        {
                            await _applicationDbContext.RolePermissions.AddAsync(newRolePermission);

                            await _applicationDbContext.SaveChangesAsync();
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(e.Message);
                            return(Json(new { success = false, message = "Error on save!" }));
                        }
                    }
                }
            }

            return(Json(new { success = true, message = "Save success!!!" }));
        }