public async Task <AppUser> AddSystemUser ( AppKey appKey, string machineName, IHashedPassword password, DateTimeOffset timeAdded ) { var user = await Add ( AppUserName.SystemUser(appKey, machineName), password, new PersonName($"{appKey.Name.DisplayText.Replace(" ", "")} {appKey.Type.DisplayText.Replace(" ", "")} {machineName}"), new EmailAddress(""), timeAdded ); var app = await factory.Apps().App(appKey); var role = await app.Role(AppRoleName.System); await user.AddRole(role); return(user); }
public Task <AppUser[]> SystemUsers(AppKey appKey) => factory.DB .Users .Retrieve() .Where(u => u.UserName.StartsWith(AppUserName.SystemUser(appKey, "").Value)) .Select(u => factory.User(u)) .ToArrayAsync();
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()); } }
public HubClientUser(HubAppClient hubClient, HubClientAppContext appContext, AppUserModel model) { this.hubClient = hubClient; this.appContext = appContext; ID = new EntityID(model.ID); userName = new AppUserName(model.UserName); }
public async Task <int> Execute(AddUserModel model) { var userName = new AppUserName(model.UserName); var hashedPassword = hashedPasswordFactory.Create(model.Password); var timeAdded = clock.Now(); var user = await appFactory.Users().Add(userName, hashedPassword, timeAdded); return(user.ID.Value); }
private async Task addUser(IHubActionTester tester, AppUserName userName) { var hubApi = tester.Services.GetService <HubAppApi>(); await hubApi.Users.AddUser.Execute(new AddUserModel { UserName = userName.Value, Password = "******" }); }
public async Task <AppUser> User(AppUserName userName) { var record = await user(userName); if (record == null) { record = await user(AppUserName.Anon); } return(factory.User(record)); }
public async Task ShouldGetUsers() { var tester = await setup(); var userName = new AppUserName("Test.User"); await addUser(tester, userName); var factory = tester.Services.GetService <AppFactory>(); var users = (await factory.Users().Users()).ToArray(); Assert.That(users.Select(u => u.UserName()), Has.One.EqualTo(userName), "Should get all users"); }
public async Task ShouldSetUserName() { var services = await setup(AppVersionKey.Current); var userName = new AppUserName("someone"); var userContext = services.GetRequiredService <FakeUserContext>(); userContext.SetCurrentUser(userName); var pageContext = await execute(services); Assert.That(pageContext.IsAuthenticated, Is.True, "Should be authenticated"); Assert.That(pageContext.UserName, Is.EqualTo(userName.Value), "Should set user name"); }
public async Task <AppUser> Verify(AppUserName userName, IHashedPassword hashedPassword) { var user = await factory.Users().User(userName); if (!user.Exists()) { throw new UserNotFoundException(userName.DisplayText); } if (!user.IsPasswordCorrect(hashedPassword)) { throw new PasswordIncorrectException(userName.DisplayText); } return(user); }
public async Task ShouldNotStartNewSession_WhenSessionHasAlreadyBeenStarted() { var input = await setup(); var userName = new AppUserName("xartogg"); input.UserContext.SetCurrentUser(userName); input.TestAuthOptions.IsEnabled = true; input.TestAuthOptions.SessionKey = Guid.NewGuid().ToString("N"); input.TestAuthOptions.User = input.UserContext.User(userName); await input.GetAsync("/Fake/Current/Controller1/Action1"); var sessionFiles = input.CurrentAction.TempLog?.StartSessionFiles(input.Clock.Now().AddMinutes(1)).ToArray() ?? new ITempLogFile[0]; Assert.That(sessionFiles.Length, Is.EqualTo(0), "Should not start session when session key exists"); }
public async Task <AppUserModel> Execute(AddSystemUserRequest model) { var systemUser = await appFactory.Users().SystemUser(model.AppKey, model.MachineName); var hashedPassword = hashedPasswordFactory.Create(model.Password); if (systemUser.UserName().Equals(AppUserName.SystemUser(model.AppKey, model.MachineName))) { await systemUser.ChangePassword(hashedPassword); } else { systemUser = await appFactory.Users().AddSystemUser ( model.AppKey, model.MachineName, hashedPassword, clock.Now() ); } var app = await appFactory.Apps().App(model.AppKey); if (!app.Key().Equals(model.AppKey)) { app = await appFactory.Apps().Add(model.AppKey, "", clock.Now()); } var systemRole = await app.Role(AppRoleName.System); if (systemRole.ID.IsNotValid()) { systemRole = await app.AddRole(AppRoleName.System); } var modifier = await app.DefaultModifier(); var assignedRoles = await systemUser.AssignedRoles(modifier); if (!assignedRoles.Any(r => r.Name().Equals(AppRoleName.System))) { await systemUser.AddRole(systemRole); } return(systemUser.ToModel()); }
public async Task <AppUser> Add ( AppUserName userName, IHashedPassword password, PersonName name, EmailAddress email, DateTimeOffset timeAdded ) { var newUser = new AppUserRecord { UserName = userName.Value, Password = password.Value(), Name = name.Value, Email = email.Value, TimeAdded = timeAdded }; await factory.DB.Users.Create(newUser); return(factory.User(newUser)); }
public Task <AppUser> SystemUser(AppKey appKey, string machineName) => User(AppUserName.SystemUser(appKey, machineName));
public async Task <IAppUser> User(AppUserName userName) { var user = await hubClient.UserInquiry.GetUserByUserName(userName.Value); return(new HubClientUser(hubClient, appContext, user)); }
private Task <AppUserRecord> user(AppUserName userName) => factory.DB .Users .Retrieve() .FirstOrDefaultAsync(u => u.UserName == userName.Value);
public Task <IAppUser> User(AppUserName userName) => userContext.User(userName);
public Task <AppUser> Add ( AppUserName userName, IHashedPassword password, DateTimeOffset timeAdded ) => Add(userName, password, new PersonName(""), new EmailAddress(""), timeAdded);
public async Task <IAppUser> User(AppUserName userName) => await appFactory.Users().User(userName);
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); } } }