public async Task <IActionResult> Register(RegisterModel model)
        {
            var existing = await _context.Users
                           .FirstOrDefaultAsync(u => u.EmailAddress == model.EmailAddress);

            if (existing != null)
            {
                return(Generate(HttpStatusCode.BadRequest, $"User name \"{model.EmailAddress}\" is already taken."));
            }

            var user = new User
            {
                EmailAddress = model.EmailAddress,
                Password     = _hashService.GenerateHash(model.Password),
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                DateOfBirth  = model.DateOfBirth.Value.ToUniversalTime()
            };

            _context.Users.Add(user);

            await _context.SaveChangesAsync();

            var result = new UserCreatedResponse
            {
                UserId = user.Id
            };

            return(Generate(HttpStatusCode.OK, result, "User successfully registered."));
        }
Exemple #2
0
        public async Task <IActionResult> UpdateProfile(UpdateProfileModel model)
        {
            var user = await _context.Users
                       .FirstOrDefaultAsync(u => u.Id == CurrentUserId);

            if (user == null || user.IsLockedOut)
            {
                return(Generate(HttpStatusCode.NotFound, "User not found."));
            }


            if (!model.EmailAddress.Equals(user.EmailAddress, StringComparison.InvariantCultureIgnoreCase))
            {
                var existing = await _context.Users
                               .FirstOrDefaultAsync(u => u.EmailAddress == model.EmailAddress);

                if (existing != null)
                {
                    return(Generate(HttpStatusCode.BadRequest, $"User name \"{model.EmailAddress}\" is already taken."));
                }
            }

            user.EmailAddress = model.EmailAddress;
            user.FirstName    = model.FirstName;
            user.LastName     = model.LastName;
            user.DateOfBirth  = model.DateOfBirth.Value.ToUniversalTime();

            await _context.SaveChangesAsync();

            return(Generate(HttpStatusCode.OK, "Your profile has been updated."));
        }
Exemple #3
0
        public async Task <IActionResult> CreateUser(CreateUserModel model)
        {
            var existing = await _context.Users
                           .FirstOrDefaultAsync(u => u.EmailAddress == model.EmailAddress);

            if (existing != null)
            {
                return(Generate(HttpStatusCode.BadRequest, $"User name \"{model.EmailAddress}\" is already taken."));
            }

            var user = new User
            {
                EmailAddress = model.EmailAddress,
                Password     = _hashService.GenerateHash(model.Password),
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                DateOfBirth  = model.DateOfBirth.Value.ToUniversalTime()
            };

            user.UserRoles.Clear();

            var roles = await _context.Roles
                        .Where(r => model.Roles.Contains(r.Name))
                        .ToListAsync();

            foreach (var role in roles)
            {
                user.UserRoles.Add(new UserRole {
                    RoleId = role.Id
                });
            }

            _context.Users.Add(user);

            await _context.SaveChangesAsync();

            var result = new UserCreatedResponse
            {
                UserId = user.Id
            };

            return(Generate(HttpStatusCode.OK, result, "User successfully created."));
        }
Exemple #4
0
        private async Task <int> AddRoleAsync(string name)
        {
            var role = await _context.Roles
                       .FirstOrDefaultAsync(u => u.Name == name);

            if (role == null)
            {
                role = new Role
                {
                    Name = name
                };

                _context.Roles.Add(role);

                await _context.SaveChangesAsync();
            }

            return(role.Id);
        }