Exemplo n.º 1
0
        private static async Task addUser(IServiceProvider sp, UserOptions userOptions)
        {
            if (string.IsNullOrWhiteSpace(userOptions.UserName))
            {
                throw new ArgumentException("User name is required");
            }
            var    appFactory            = sp.GetService <AppFactory>();
            var    clock                 = sp.GetService <Clock>();
            var    hashedPasswordFactory = sp.GetService <IHashedPasswordFactory>();
            string password;

            if (string.IsNullOrWhiteSpace(userOptions.Password))
            {
                password = Guid.NewGuid().ToString("N") + "!?";
            }
            else
            {
                password = userOptions.Password;
            }
            var userName       = new AppUserName(userOptions.UserName);
            var hashedPassword = hashedPasswordFactory.Create(password);
            var user           = await appFactory.Users().User(userName);

            if (user.Exists())
            {
                await user.ChangePassword(hashedPassword);
            }
            else
            {
                await appFactory.Users().Add(userName, hashedPassword, clock.Now());
            }
        }
Exemplo n.º 2
0
 private static async Task storeCredentials(IServiceProvider sp, UserOptions userOptions)
 {
     if (string.IsNullOrWhiteSpace(userOptions.CredentialKey))
     {
         throw new ArgumentException("Credential Key is required");
     }
     if (string.IsNullOrWhiteSpace(userOptions.UserName))
     {
         throw new ArgumentException("User name is required");
     }
     if (string.IsNullOrWhiteSpace(userOptions.Password))
     {
         throw new ArgumentException("Password is required");
     }
     var secretCredentialsFactory = sp.GetService <SecretCredentialsFactory>();
     var secretCredentials        = secretCredentialsFactory.Create(userOptions.CredentialKey);
     await secretCredentials.Update
     (
         new CredentialValue(userOptions.UserName, userOptions.Password)
     );
 }
Exemplo n.º 3
0
        private static async Task addSystemUser(IServiceProvider sp, UserOptions userOptions)
        {
            if (string.IsNullOrWhiteSpace(userOptions.AppName))
            {
                throw new ArgumentException("App name is required");
            }
            if (string.IsNullOrWhiteSpace(userOptions.AppType))
            {
                throw new ArgumentException("App type is required");
            }
            if (string.IsNullOrWhiteSpace(userOptions.MachineName))
            {
                throw new ArgumentException("Machine name is required");
            }
            string password;

            if (string.IsNullOrWhiteSpace(userOptions.Password))
            {
                password = Guid.NewGuid().ToString("N") + "!?";
            }
            else
            {
                password = userOptions.Password;
            }
            var appKey = new AppKey
                         (
                new AppName(userOptions.AppName),
                AppType.Values.Value(userOptions.AppType)
                         );
            var hubApi = sp.GetService <HubAppApi>();
            await hubApi.AppRegistration.AddSystemUser.Invoke(new AddSystemUserRequest
            {
                AppKey      = appKey,
                MachineName = userOptions.MachineName,
                Password    = password
            });
        }
Exemplo n.º 4
0
        private static async Task addUserRoles(IServiceProvider sp, UserOptions userOptions)
        {
            if (string.IsNullOrWhiteSpace(userOptions.UserName))
            {
                throw new ArgumentException("User name is required");
            }
            if (string.IsNullOrWhiteSpace(userOptions.AppName))
            {
                throw new ArgumentException("App name is required");
            }
            if (string.IsNullOrWhiteSpace(userOptions.AppType))
            {
                throw new ArgumentException("App type is required");
            }
            var appFactory = sp.GetService <AppFactory>();
            var userName   = new AppUserName(userOptions.UserName);
            var user       = await appFactory.Users().User(userName);

            var appType = string.IsNullOrWhiteSpace(userOptions.AppType)
                ? AppType.Values.WebApp
                : AppType.Values.Value(userOptions.AppType);
            var app = await appFactory.Apps().App(new AppKey(userOptions.AppName, appType));

            var roles = new List <AppRole>();

            if (!string.IsNullOrWhiteSpace(userOptions.RoleNames))
            {
                foreach (var roleName in userOptions.RoleNames.Split(","))
                {
                    if (!string.IsNullOrWhiteSpace(roleName))
                    {
                        var role = await app.Role(new AppRoleName(roleName));

                        if (role.Exists())
                        {
                            roles.Add(role);
                        }
                    }
                }
            }
            Modifier modifier;

            if (string.IsNullOrWhiteSpace(userOptions.ModKey))
            {
                modifier = await app.DefaultModifier();
            }
            else
            {
                var modCategory = await app.ModCategory(new ModifierCategoryName(userOptions.ModCategoryName));

                modifier = await modCategory.Modifier(userOptions.ModKey);
            }
            var userRoles = (await user.ExplicitlyAssignedRoles(modifier)).ToArray();

            foreach (var role in roles)
            {
                if (!userRoles.Any(ur => ur.ID.Equals(role.ID)))
                {
                    await user.AddRole(role);
                }
            }
        }