public async Task <IActionResult> OnPostAsync()
        {
            var isAuthorize = User.IsInRole(Constants.AdministratorRole);

            if (!isAuthorize)
            {
                return(new ChallengeResult());
            }

            WorkerCrossPageData = await WorkerCrossPage.FindWorkerDataById(_context, Account.WorkerId);

            if (!ModelState.IsValid)
            {
                return(await LoadPageData(Account.WorkerId));
            }

            var role = await _roleManager.FindByIdAsync(Account.RoleId);

            if (role == null)
            {
                return(await LoadPageData(Account.WorkerId));
            }

            var user = new IdentityUser
            {
                UserName = Account.Email,
                Email    = Account.Email,
            };

            var createUserStatus = await _userManager.CreateAsync(user, Account.Password);

            if (!createUserStatus.Succeeded)
            {
                foreach (var err in createUserStatus.Errors)
                {
                    ModelState.AddModelError(string.Empty, err.Description);
                }

                return(await LoadPageData(Account.WorkerId));
            }

            var addToRoleStatus = await _userManager.AddToRoleAsync(user, role.Name);

            if (!addToRoleStatus.Succeeded)
            {
                foreach (var err in addToRoleStatus.Errors)
                {
                    ModelState.AddModelError(string.Empty, err.Description);
                }

                return(await LoadPageData(Account.WorkerId));
            }

            await _context.AddUserToWorkerAsync(Account.WorkerId, user.Id);

            return(RedirectToPage("./EditAccount", new { id = Account.WorkerId }));
        }
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var isAuthorized = User.IsInRole(Constants.AdministratorRole);

            if (!isAuthorized)
            {
                return(new ChallengeResult());
            }

            PageData = await
                           (from worker in _context.Workers
                           join workerUser in _context.WorkerUsers
                           on worker.WorkerId equals workerUser.WorkerId
                           where worker.WorkerId == id
                           select new AccountData
            {
                Firstname  = worker.Firstname,
                Lastname   = worker.Lastname,
                Patronymic = worker.Patronymic,
                WorkerID   = worker.WorkerId,
                UserID     = workerUser.IdentityUserId,
            }).AsNoTracking().FirstOrDefaultAsync();

            if (PageData == null)
            {
                WorkerCrossPageData = await WorkerCrossPage.FindWorkerDataById(_context, id.Value);
            }
            else
            {
                var user = await _userManager.FindByIdAsync(PageData.UserID);

                var roles = await _userManager.GetRolesAsync(user);

                PageData.Login      = user.UserName;
                PageData.Role       = roles.Count == 0 ? "Not exist" : roles[0];
                WorkerCrossPageData = PageData;
            }

            if (WorkerCrossPageData == null)
            {
                return(NotFound());
            }

            return(Page());
        }
        private async Task <PageResult> LoadPageData(int id)
        {
            WorkerCrossPageData = await WorkerCrossPage.FindWorkerDataById(_context, id);

            RoleSelectList = await
                                 (from role in _roleManager.Roles
                                 select new SelectListItem
            {
                Text  = role.Name,
                Value = role.Id
            }).AsNoTracking().ToListAsync();

            return(Page());
        }
Ejemplo n.º 4
0
        private async Task <IActionResult> RedisplayPage(int id)
        {
            WorkerCrossPage = await WorkerCrossPage.FindWorkerDataById(_context, id);

            if (WorkerCrossPage == null)
            {
                return(NotFound());
            }

            Positions = await Position.GetSelectListItems(_context);

            if (Positions == null)
            {
                return(NotFound());
            }

            return(Page());
        }
        private async Task <PageResult> RedisplayPage(int id)
        {
            WorkerData = await WorkerCrossPage.FindWorkerDataById(_context, id);

            return(Page());
        }