public async Task <IActionResult> Create(Profile obj, string roleName, int tariffId)
        {
            await ServerSideValidation(obj, roleName, tariffId);

            if (ModelState.IsValid)
            {
                obj.Tariff = await DbContext.Tariffs
                             .FirstOrDefaultAsync(t => t.Id.Equals(tariffId));

                var result = await UserMngr.CreateAsync(obj.User);

                if (result.Succeeded)
                {
                    var role = await RoleMngr.FindByNameAsync(roleName);

                    await UserMngr.AddToRoleAsync(obj.User, role.Name);
                }

                await DbContext.Profiles.AddAsync(obj);

                await DbContext.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(await AddUpdateForm());
        }
Exemple #2
0
        public async Task ServerSideValidation(AccessRight obj)
        {
            TryValidateModel(obj);
            ModelState.Remove("Role.LocalizedName");
            ModelState.Remove("Role.DefaultControllerName.Name");
            var cntrlExist = await DbContext.ControllersNames
                             .AnyAsync(c => c.Name.Equals(obj.Controller.Name));

            if (!cntrlExist)
            {
                ModelState.AddModelError("ControllerName", "Выбранная страница не существует");
            }
            var role = await RoleMngr.FindByNameAsync(obj.Role.Name);

            if (role == null)
            {
                ModelState.AddModelError("Role", "Выбранная роль не существует");
            }
            if (!ActionName.Equals(nameof(Change)))
            {
                var accessRightExisting = DbContext.AccessRights.Include("Role")
                                          .FirstOrDefault(ar => ar.Role.Name.Equals(obj.Role.Name) &&
                                                          ar.Controller.Name.Equals(obj.Controller.Name));
                if (accessRightExisting != null)
                {
                    ModelState.AddModelError("", "Правило для этой роли и страницы уже есть, измените его");
                }
            }
        }
        public async Task <IActionResult> Update(Profile obj, string roleName, int tariffId)
        {
            await ServerSideValidation(obj, roleName, tariffId);

            if (ModelState.IsValid)
            {
                var accountExisting = await DbContext.Users
                                      .FirstOrDefaultAsync(u => u.Id.Equals(obj.User.Id));

                var profileExisting = await DbContext.Profiles.AsNoTracking()
                                      .FirstOrDefaultAsync(p => p.Id.Equals(obj.Id));

                accountExisting.UserName = obj.User.UserName;
                accountExisting.Email    = obj.User.Email;
                obj.User   = accountExisting;
                obj.Tariff = await DbContext.Tariffs
                             .FirstOrDefaultAsync(t => t.Id.Equals(tariffId));

                var role = await RoleMngr.FindByNameAsync(roleName);

                await UserMngr.RemoveFromRoleAsync(obj.User, role.Name);

                await UserMngr.AddToRoleAsync(obj.User, role.Name);

                await Task.Run(() =>
                {
                    if (profileExisting.IsEnabled != obj.IsEnabled)
                    {
                        _tariffRegulator.StartToUseOfTariff(obj);
                    }

                    obj.DateOfUpdate = DateTime.Now;
                    DbContext.Update(obj);
                    DbContext.SaveChanges();
                });

                return(RedirectToAction("Index"));
            }

            return(await AddUpdateForm(obj.Id));
        }
Exemple #4
0
        public async Task <IActionResult> Create(AccessRight obj, List <bool> rights)
        {
            await ServerSideValidation(obj);

            if (ModelState.IsValid)
            {
                obj.RoleId     = (await RoleMngr.FindByNameAsync(obj.Role.Name)).Id;
                obj.Role       = null;
                obj.Controller = await DbContext.ControllersNames
                                 .FirstOrDefaultAsync(c => c.Name.Equals(obj.Controller.Name));

                obj.UpdateActionsRights(rights);
                await DbContext.AccessRights.AddAsync(obj);

                await DbContext.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(await AddUpdateForm());
        }
Exemple #5
0
        public async Task <IActionResult> CheckRoleExist([NotNull] string role)
        {
            var roleExisting = await RoleMngr.FindByNameAsync(role);

            return(Json((roleExisting != null)?true:false));
        }