/// <summary> /// Adds a collection of users and roles to the system. /// </summary> /// <param name="usernames">Usernames to add.</param> /// <param name="roleNames">Roles to add the usernames to.</param> public override void AddUsersToRoles(string[] usernames, string[] roleNames) { for (int x = 0; x < usernames.Length; x++) { String currentName = roleNames[x]; if (db.Role.Where(m => m.roleName == currentName).Count() < 1) { CreateRole(currentName); } RoleUser user = new RoleUser(); user.role = db.Role.Where(m => m.roleName == currentName).FirstOrDefault(); user.userName = usernames[x]; db.RoleUser.Add(user); db.SaveChanges(); } }
/// <summary> /// Adds a singular user to a role. /// </summary> /// <param name="userName">User to add.</param> /// <param name="roleName">Role to add the user to.</param> public void AddUserToRole(string userName, string roleName) { if (db.Role.Where(m => m.roleName == roleName).Count() < 1) { CreateRole(roleName); } if (db.RoleUser.Where(m => m.userName == userName).Where(m => m.role.roleName == roleName).Count() == 0) { return; } RoleUser user = new RoleUser(); user.role = db.Role.Where(m => m.roleName == roleName).FirstOrDefault(); user.userName = userName; db.RoleUser.Add(user); db.SaveChanges(); }
/// <summary> /// Saves a new superAdmin to the database. /// </summary> /// <param name="userName">Username of the superAdmin to save.</param> public void saveNewAdmin(string userName) { if (db.RoleUser.Where(m => m.userName == userName).Where(m => m.role.roleName == "superAdmin").Count() > 0) { return; } RoleUser user = new RoleUser(); user.role = db.Role.Where(m => m.roleName == "superAdmin").FirstOrDefault(); user.userName = userName; db.RoleUser.Add(user); db.SaveChanges(); }