コード例 #1
0
ファイル: UsersController.cs プロジェクト: Olly14/Limdo
        public async Task <IActionResult> PutUser(string id, User user)
        {
            if (id != user.SubjectId)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Register([FromBody] User credentials)
        {
            var user = new IdentityUser {
                UserName = credentials.Email, Email = credentials.Email
            };


            try {
                var result = await userManager.CreateAsync(user, credentials.Password);

                if (!result.Succeeded)
                {
                    return(BadRequest(result.Errors));
                }


                await signInManager.SignInAsync(user, isPersistent : false);

                _context.User.Add(credentials);
                await _context.SaveChangesAsync();

                return(Ok(CreateToken(user)));
            } catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #3
0
        public async Task ApprovePendingReportingManagerRequests(
            ApproveReportingManagerPendingRequest request)
        {
            var pendingRequest = await _context.ReportingManagerRequests
                                 .Where(x => x.RequestStatus == false &&
                                        x.ReportingManagerRequestId == request.ReportingManagerRequestId)
                                 .SingleOrDefaultAsync();

            if (null != pendingRequest)
            {
                var user = await _userContext.Users.FirstOrDefaultAsync(x => x.Email == request.ReportedUserEmail);

                if (null != user)
                {
                    user.ReportingManagerId      = request.NewReportingManagerId;
                    pendingRequest.RequestStatus = true;
                    await _userContext.SaveChangesAsync();

                    await _context.SaveChangesAsync();
                }
            }
        }
        public async Task DeleteDepartment(int departmentId)
        {
            var existingDepartment = await _context.Departments.SingleOrDefaultAsync(r => r.Id == departmentId);

            if (null != existingDepartment)
            {
                _context.Remove(existingDepartment);
                await _context.SaveChangesAsync();
            }

            var departmentUsers = await _userContext.Users.Where(x => x.DepartmentId == departmentId).ToListAsync();

            if (departmentUsers.Any())
            {
                foreach (var departmentUser in departmentUsers)
                {
                    departmentUser.DepartmentId = 0;
                }

                await _userContext.SaveChangesAsync();
            }
        }
コード例 #5
0
        public async Task DeleteRole(int roleId)
        {
            var existingRole = await _context.Roles.SingleOrDefaultAsync(r => r.RoleId == roleId);

            if (null != existingRole)
            {
                _context.Remove(existingRole);
                await _context.SaveChangesAsync();
            }

            var userBelongingToRole = await _userContext.Users.Where(x => x.RoleId == roleId).ToListAsync();

            if (userBelongingToRole.Any())
            {
                foreach (var roleUser in userBelongingToRole)
                {
                    roleUser.RoleId = 0;
                }

                await _userContext.SaveChangesAsync();
            }
        }
コード例 #6
0
        public static async Task Initial(IServiceProvider provider)
        {
            UserIdentityDbContext context   = provider.GetRequiredService <UserIdentityDbContext>();
            Encryptor             encryptor = new Encryptor();

            if (!context.Users.Any())
            {
                for (int i = 0; i < 20; i++)
                {
                    string       salt = SaltGenerator.GenerateSalt();
                    UserIdentity user = new UserIdentity()
                    {
                        UserName = $"User{i+1}",
                        Password = encryptor.Encrypt($"User{i+1}", salt),
                        Salt     = salt,
                        Id       = i + 1,
                        Email    = $"User{i + 1}@cnblog.com"
                    };
                    await context.Users.AddAsync(user);

                    await context.SaveChangesAsync();
                }
            }
        }