Exemplo n.º 1
0
        public async Task <bool> CreateUserIfNotExists(string username, string password, params string[] permissions)
        {
            var exists = await _securityRepository.Exists <ApplicationUser>(u => u.UserName == username);

            if (exists)
            {
                Log(true, nameof(ApplicationUser), username);
            }
            else
            {
                Log(false, nameof(ApplicationUser), username);
                var user = CreateUser(username, password, false);
                await _securityRepository.AddUser(user);

                var permissionsToAdd = AppendPermissionsDistinct(permissions, IdentityScopes);
                var claims           = permissionsToAdd.Select(permission => CreateClaim("permission", permission)).ToList();
                claims.Add(CreateClaim("subject", user.UserName));

                foreach (var claim in claims)
                {
                    claim.UserId = user.Id;
                    await _securityRepository.AddClaim(claim);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public void AddUser(User userToAdd, ref ValidationStateDictionary states)
        {
            var v = new UserValidator().Validate(userToAdd, validationRepositoryInstance);

            if (v.Errors.Count > 0)
            {
                states.Add(typeof(User), v);
                return;
            }

            var settings = SecurityConfig.GetCurrent();

            if (settings.Cookie.PasswordHashed && !string.IsNullOrEmpty(userToAdd.Password))
            {
                userToAdd.Password = PasswordHash.Hash(userToAdd.Username.ToLower(), userToAdd.Password);
            }
            else if (userToAdd.AccountType == Constants.AccountType.ADLocal)
            {
                userToAdd.Password = "******";
            }
            userToAdd.ApprovalStatus = Constants.ApprovalStatus.Pending;
            userToAdd.IsFirstLogIn   = true;
            userToAdd.RoleId         = userToAdd.UserRole.RoleId;
            //check if the AccountExpires and set the number of days for Accountexpiry

            updateUserAccountExpiry(userToAdd);
            repositoryInstance.AddUser(userToAdd);
            RegistrationNotification(userToAdd, true);
        }
Exemplo n.º 3
0
        public void SaveUser(User user)
        {
            var standardRole     = _securityRepository.GetAllRoles().Where(r => r.Name == "GeneralUsers").FirstOrDefault();
            var securityUserRole = new SecurityUserRole();

            securityUserRole.SecurityRole = standardRole;
            securityUserRole.User         = user;
            user.Roles.Add(securityUserRole);
            _securityRepository.AddUser(user);
        }
Exemplo n.º 4
0
 public UserProfile PostUser([FromBody] UserProfile user)
 {
     return(securityEntity.GetUser(securityEntity.AddUser(user)));
 }