public async Task AddUserActivity(int userId, string actionType)
        {
            if (!Enum.TryParse(actionType, out UserActionType actionTypeParsed))
            {
                throw new ValidationException(Constants.ServiceMessages.ADD_USER_ACTIVITY_INVALID_ACTION_TYPE);
            }

            if (!await _dbContext.Users.AnyAsync(u => u.Id == userId))
            {
                throw new ValidationException(Constants.ServiceMessages.ADD_USER_ACTIVITY_INVALID_USER);
            }

            _dbContext.UserActivityLogs.Add(new UserActivityLog
            {
                UserId     = userId,
                ActionType = actionType,
                EntryDate  = DateTime.Now
            });
            await _dbContext.SaveChangesAsync();
        }
예제 #2
0
        public async Task <UserDto> CreateAsync(UserCreateModel model)
        {
            if (await _dbContext.Users.AnyAsync(u => u.UserName == model.UserName))
            {
                throw new ValidationException(Constants.ServiceMessages.CREATE_USER_USERNAME_IN_USE);
            }
            if (await _dbContext.UserProfiles.AnyAsync(up => up.EmailAddress == model.Profile.EmailAddress))
            {
                throw new ValidationException(Constants.ServiceMessages.CREATE_USER_EMAIL_IN_USE);
            }

            if (model.Roles == null || !model.Roles.Any())
            {
                throw new ValidationException(Constants.ServiceMessages.CREATE_USER_NO_ROLE);
            }

            var validRoles = await _dbContext.Roles.Select(r => r.Id).ToListAsync();

            if (model.Roles.Except(validRoles).Any())
            {
                throw new ValidationException(Constants.ServiceMessages.CREATE_USER_INVALID_ROLE);
            }

            var userEntity = new User
            {
                UserName  = model.UserName,
                Password  = _passwordHelper.HashPassword(model.Password),
                CreatedOn = DateTime.Now,
                IsActive  = true,
                Roles     = model.Roles.Select(r => new UserRole {
                    RoleId = r
                }).ToList()
            };

            _dbContext.Users.Add(userEntity);
            await _dbContext.SaveChangesAsync();

            return(userEntity.ToDto(false));
        }
예제 #3
0
        public async Task ToggleResourceIdAccess(string policyName, bool isRequired, IIamProviderCache cache)
        {
            var policyId = await CreateOrGetPolicy(policyName);

            var policy = await _context.IamPolicyResourceIds
                         .FirstOrDefaultAsync(x => x.Id.Equals(policyId));

            if (policy == null)
            {
                _context.IamPolicyResourceIds.Add(new Model.PolicyResourceId <TKey>()
                {
                    PolicyId = policyId,
                    RequiresResourceIdAccess = isRequired
                });
            }
            else
            {
                policy.RequiresResourceIdAccess = isRequired;
            }

            await _context.SaveChangesAsync();

            cache.ToggleResourceIdAccess(policyName, isRequired);
        }