Пример #1
0
        public async Task <ActionResult <UserResponse> > Add([FromBody] UserRequest request)
        {
            if (request == null)
            {
                return(BadRequest(new { errorText = "No request data." }));
            }

            if (_context.Users.Any(u => u.Login == request.Login))
            {
                return(BadRequest(new { errorText = "User with that login already exists." }));
            }

            if (!_context.UserStatus.Any(us => us.UserStatusId == request.UserStatusId))
            {
                return(BadRequest(new { errorText = "Wrong UserStatusId." }));
            }

            int userRole = _context.Roles.First(r => string.Equals(r.Name, "User", StringComparison.CurrentCultureIgnoreCase)).RoleId;

            User newUser = new User()
            {
                Login        = request.Login,
                Password     = HashPasswordGenerator.GenerateHash(request.Password),
                CoreId       = Guid.NewGuid(),
                UserStatusId = request.UserStatusId
            };

            _context.Users.Add(newUser);

            _context.SaveChanges();

            UserRole newUserRole = new UserRole()
            {
                UserId = newUser.UserId,
                RoleId = userRole
            };

            _context.UserRoles.Add(newUserRole);


            await _context.SaveChangesAsync();

            Response.StatusCode = 201;
            return(new UserResponse()
            {
                Login = newUser.Login,
                Password = newUser.Password,
                UserStatusId = newUser.UserStatusId,
                CoreId = newUser.CoreId
            });
        }
Пример #2
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Setting default data into variable
            const int    adminRoleId    = 1;
            const int    activeStatusId = 1;
            const long   adminUserId    = 1;
            const int    userRoleId     = 1;
            const string adminLogin     = "******";
            const string adminPassword  = "******";

            // Sets default user roles in VDCore.Roles table
            Role adminRole = new Role()
            {
                RoleId = adminRoleId, Name = "Administrator"
            };
            Role userRole = new Role()
            {
                RoleId = adminRoleId + 1, Name = "User"
            };

            modelBuilder.Entity <Role>().HasData(adminRole, userRole);

            // Sets default user roles in VDCore.UserStatus table
            UserStatus activeStatus = new UserStatus()
            {
                UserStatusId = activeStatusId, StatusName = "Active"
            };
            UserStatus disabledStatus = new UserStatus()
            {
                UserStatusId = activeStatusId + 1, StatusName = "Disabled"
            };

            modelBuilder.Entity <UserStatus>().HasData(activeStatus, disabledStatus);

            // Adds default admin user
            User adminUser = new User()
            {
                UserId       = adminUserId,
                Login        = adminLogin,
                Password     = HashPasswordGenerator.GenerateHash(adminPassword),
                CoreId       = Guid.NewGuid(),
                UserStatusId = activeStatus.UserStatusId
            };

            modelBuilder.Entity <User>().HasData(adminUser);
            modelBuilder.Entity <UserRole>().HasData(new UserRole()
            {
                Id = userRoleId, RoleId = adminRole.RoleId, UserId = adminUser.UserId
            });
        }
Пример #3
0
        public async Task <ActionResult <UserResponse> > Update([FromBody] UserUpdateRequest request)
        {
            if (request == null)
            {
                return(BadRequest(new { errorText = "No request data." }));
            }

            if (!_context.Users.Any(x => x.CoreId == Guid.Parse(request.CoreId)))
            {
                return(NotFound(new { errorText = "User with coreId " + request.CoreId + " is not found." }));
            }

            User userForUpdate = _context.Users.First(u => u.CoreId == Guid.Parse(request.CoreId));

            if (_context.Users.Any(u => u.Login == request.Login))
            {
                return(BadRequest(new { errorText = "User with that login already exists." }));
            }

            if (!_context.UserStatus.Any(us => us.UserStatusId == request.UserStatusId))
            {
                return(BadRequest(new { errorText = "Wrong UserStatusId." }));
            }

            // Updating user data.
            userForUpdate.Login        = request.Login;
            userForUpdate.Password     = HashPasswordGenerator.GenerateHash(request.Password);
            userForUpdate.UserStatusId = request.UserStatusId;
            _context.Update(userForUpdate);

            await _context.SaveChangesAsync();

            return(Ok(new UserResponse()
            {
                Login = userForUpdate.Login,
                Password = userForUpdate.Password,
                UserStatusId = userForUpdate.UserStatusId,
                CoreId = userForUpdate.CoreId
            }));
        }