public void Homepage() { TestStore store = ((TestStore)DevWebsiteDataInitializer.FillDefaultDevSet(new TestStore())).FixupReferences(); var org = store.Organizations.First(); // Setup User u = new User(); var perms = new Mock<TestAuthIdentityService>(); perms.Setup(f => f.UserLogin).Returns("testuser"); perms.Setup(f => f.UserName).Returns("Test User"); perms.Setup(f => f.IsAuthenticated).Returns(true); perms.Setup(f => f.HasPermission(PermissionType.SiteAdmin, null)).Returns(true); perms.Setup(f => f.IsUser).Returns(true); perms.Setup(f => f.User).Returns(u); // perms.Setup(f => f.GetRolesForUser("testuser", true)).Returns(new[] { "role1", "role2", "org" + org.Id.ToString() + ".foo" }); var controller = new HomeController(perms.Object, new DataStoreFactory(store)); //var mocks = new ContextMocks(controller); //mocks.Request.Setup(r => r.AcceptTypes).Returns(new[] { "application/json" }); //Test var result = controller.Index(); // Verify Assert.IsNotNull(result as ViewResult, "result is ViewResult"); var view = (ViewResult)result; Assert.IsNotNull(view.Model, "Did not return model"); Assert.IsNotNull(view.Model as HomePageViewModel); var model = (HomePageViewModel)view.Model; Assert.IsTrue(model.HasAccount); Assert.AreEqual(Guid.Empty, model.LinkedMember); Assert.IsTrue(model.LoggedIn); Assert.IsFalse(model.MyDetails); Assert.IsFalse(model.MyTraining); Assert.IsFalse(model.MyMissions); }
private ActionResult RegisterAccount(RegisterModel model) { if (model.Password != model.ConfirmPassword) { ModelState.AddModelError("ConfirmPassword", "Passwords do not match"); } if (ModelState.IsValid) { using (var ctx = this.GetRepository()) { User u = ctx.Users.SingleOrDefault(f => f.Username == model.UserName); if (u != null) { ModelState.AddModelError("UserName", "Not available"); ModelState.SetModelValue("UserName", new ValueProviderResult(model.UserName, model.UserName, CultureInfo.CurrentUICulture)); } else { u = new User { Username = model.UserName, Name = string.Format("{0} {1}", model.FirstName, model.LastName), Email = model.Email, Password = model.Password }; try { ctx.Users.Add(AuthIdentityService.SetupUser(u)); ctx.SaveChanges(); // Send email notice to user MailService.SendMail(u.Email, "Verify SARTracks Account", string.Format("Click the link to verify your account:\n{0}?UserName={1}&Key={2}", Url.ActionFull("Verify"), u.Username, u.ValidationKey)); return RedirectToAction("Verify", "Account"); } catch { TempData["error"] = "Error saving user. Please try again."; } } } } // If we got this far, something failed, redisplay form return View(model); }
private User PopulateAuthData(TestStore store) { User user = new User { Username = "******" }; store.Users.Add(user); Role parent = new Role { Name = "Parent" }; Role child = new Role { Name = "Child" }; store.Roles.Add(parent); store.Roles.Add(child); RoleTests.MakeMember(parent, child); RoleUserMembership ru = new RoleUserMembership { User = user, Role = parent }; parent.Users.Add(ru); user.Roles.Add(ru); Authorization auth = new Authorization { Permission = PermissionType.EditMember, Role = child, RoleId = child.Id }; store.Authorization.Add(auth); return user; }
/// <summary> /// Overwrites .Password and .PasswordSalt of the specified user /// with the encrypted values for the password in .Password /// </summary> /// <param name="u"></param> /// <param name="newPassword"></param> private static void SetPassword(User u, string newPassword) { u.PasswordSalt = GenerateSalt(); u.Password = CreatePasswordHash(newPassword, u.PasswordSalt); }
internal static Models.User SetupUser(User u) { u.State = UserState.Verification; u.ValidationKey = Guid.NewGuid(); SetPassword(u, u.Password); return u; }
/// <summary> /// /// </summary> /// <param name="u"></param> /// <returns>New password in clear text</returns> public static string ResetPassword(User u) { u.PasswordSalt = GenerateSalt(); string password = Membership.GeneratePassword(8, 2); u.Password = CreatePasswordHash(password, u.PasswordSalt); return password; }
/// <summary> /// /// </summary> /// <param name="store"></param> /// <returns>The unencrypted password of the new 'admin' user</returns> public static User CreateAdminUser(string password) { string salt = GenerateSalt(); User admin = new User { Username = "******", Email = ConfigurationManager.AppSettings["adminEmail"] ?? "*****@*****.**", Name = "Admin User", PasswordSalt = salt, Password = CreatePasswordHash(password, salt), State = UserState.Okay }; return admin; }
public static bool ChangePassword(User u, string oldPassword, string newPassword) { if (CreatePasswordHash(oldPassword, u.PasswordSalt) != u.Password) { return false; } SetPassword(u, newPassword); return true; }
public AuthIdentityService(string username, DataStoreFactory storeFactory) { this.storeFactory = storeFactory; using (var ctx = storeFactory.Create(username)) { this.currentUser = ctx.Users.IncludePaths("Member").SingleOrDefault(f => f.Username == username); } this.IsUser = this.currentUser != null; this.UserLogin = username; this.UserName = (this.currentUser == null) ? username : (this.currentUser.Member == null) ? this.currentUser.Name : this.currentUser.Member.FullName; this.IsAuthenticated = !string.IsNullOrWhiteSpace(username); Init(); }
public static void InitializeOrganizationSecurity(this IDataStoreService ctx, Organization org, User admin) { org.AdminAccount = (admin == null) ? "setup" : admin.Username; var usersRole = new Role { Name = "Members", OrganizationId = org.Id, SystemRole = true }; var adminRole = new Role { Name = "Administrators", OrganizationId = org.Id, SystemRole = true }; adminRole.MemberOfRoles.Add(new RoleRoleMembership { Parent = usersRole, Child = adminRole, IsSystem = true }); var siteAdmin = ctx.Roles.Single(f => f.Name == AuthIdentityService.ADMIN_ROLE && f.OrganizationId == null); siteAdmin.MemberOfRoles.Add(new RoleRoleMembership { Parent = adminRole, Child = siteAdmin, IsSystem = true }); if (admin != null) { adminRole.Users.Add(new RoleUserMembership { Role = adminRole, User = admin }); } ctx.Roles.Add(adminRole); ctx.Roles.Add(usersRole); ctx.Authorization.Add(new Authorization { Role = adminRole, Scope = org.Id, Permission = PermissionType.AdminOrganization, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = adminRole, Scope = org.Id, Permission = PermissionType.AddOrganizationMembers, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = adminRole, Scope = org.Id, Permission = PermissionType.EditMember, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = adminRole, Scope = org.Id, Permission = PermissionType.EditMemberContacts, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = adminRole, Scope = org.Id, Permission = PermissionType.ViewMemberDetail, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = adminRole, Scope = org.Id, Permission = PermissionType.ViewMemberStandard, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = usersRole, Scope = org.Id, Permission = PermissionType.ViewOrganizationBasic, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = usersRole, Scope = org.Id, Permission = PermissionType.ViewOrganizationDetail, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = usersRole, Scope = org.Id, Permission = PermissionType.ListOrganization, IsSystem = true }); ctx.Authorization.Add(new Authorization { Role = usersRole, Scope = org.Id, Permission = PermissionType.ViewMemberStandard, IsSystem = false }); ctx.Authorization.Add(new Authorization { Role = usersRole, Scope = org.Id, Permission = PermissionType.ViewMemberBasic, IsSystem = false }); }