internal IClientApplication CreateClientApplication(string name)
        {
            Guard.NotNullOrEmpty(() => name, name);

            // Get the app
            IEnumerable <IClientApplication> apps = Storage.Find(Storage.BuildQuery(
                                                                     Reflector <ClientApplication> .GetPropertyName(x => x.Name), QueryOperator.EQ, name));

            if (apps.Any())
            {
                throw LogicErrorThrower.ResourceConflict(
                          Resources.ClientApplicationsManager_ApplicationByNameExists,
                          name);
            }

            // Construct a new identifier and a secret
            var app = new ClientApplication
            {
                Name             = name,
                ClientIdentifier = Guid.NewGuid().ToString("D"),
                ClientSecret     = Guid.NewGuid().ToString("D"),
            };

            return(RegisterClientApplication(app));
        }
示例#2
0
        internal IUserAccount CreateUserAccount(string currentUsername, string username, string passwordHash,
                                                string forenames, string surname, string email, string mobilePhone, Address address, string roles = null)
        {
            Guard.NotNull(() => currentUsername, currentUsername);
            Guard.NotNullOrEmpty(() => forenames, forenames);
            Guard.NotNullOrEmpty(() => surname, surname);
            Guard.NotNullOrEmpty(() => email, email);
            Guard.NotNull(() => address, address);

            // Check email not already registered
            if (FindUserAccount(x => x.Email, email))
            {
                throw LogicErrorThrower.ResourceConflict(Resources.UserAccountsManager_UserAccountExistsByEmail, email);
            }

            // Check username not already used
            if (CredentialsProvided(username, passwordHash) && FindUserAccount(x => x.Username, username))
            {
                throw LogicErrorThrower.ResourceConflict(Resources.UserAccountsManager_UserAccountExistsByUsername,
                                                         username);
            }

            // Can't specify role if no credentials
            if (!CredentialsProvided(username, passwordHash) && roles.HasValue())
            {
                throw new RuleViolationException(Resources.UserAccountsManager_NoRolesForParticipant);
            }

            var newAccount = new UserAccount
            {
                Username     = (CredentialsProvided(username, passwordHash)) ? username : email,
                PasswordHash = (CredentialsProvided(username, passwordHash)) ? passwordHash : null,
                Forenames    = forenames,
                Surname      = surname,
                MobilePhone  = mobilePhone,
                Address      = address,
                Email        = email,
                Roles        = CalculateRoles(username, passwordHash, roles),
                IsRegistered = CredentialsProvided(username, passwordHash),
            };

            string accountId = Storage.Add(newAccount);

            newAccount.Id = accountId;

            //TODO: Audit the creation of the user account

            return(newAccount);
        }