public override void AddUsersToRoles(string[] usernames, string[] rolenames) { using (Db4oDatabase dbc = new Db4oDatabase(connectionString)) { foreach (string rolename in rolenames) { if (!RoleExists(rolename, dbc)) throw new ProviderException("Role name not found."); } foreach (string username in usernames) { if (username.Contains(",")) throw new ArgumentException("User names cannot contain commas."); } foreach (string rolename in rolenames) { IList<Role> results = dbc.Query((Role r) => r.Rolename == rolename && r.ApplicationName == applicationName); Role found = results[0]; foreach (string username in usernames) { if (!found.EnrolledUsers.Contains(username)) found.EnrolledUsers.Add(username); } dbc.Store(found); } foreach (string username in usernames) { IList<EnrolledUser> results = dbc.Query((EnrolledUser u) => u.Username == username && u.ApplicationName == applicationName); EnrolledUser found; if (results.Count == 0) { // this is the only place in the API that EnrolledUser's // are added to the database found = new EnrolledUser(username, applicationName); } else found = results[0]; foreach (string rolename in rolenames) { if (!found.Roles.Contains(rolename)) found.Roles.Add(rolename); } dbc.Store(found); } } }
public override void RemoveUsersFromRoles(string[] usernames, string[] rolenames) { using (Db4oDatabase dbc = new Db4oDatabase(connectionString)) { foreach (string rolename in rolenames) { if (!RoleExists(rolename, dbc)) { throw new ProviderException("Role name not found."); } } foreach (string rolename in rolenames) { IList <Role> results = dbc.Query((Role r) => r.Rolename == rolename && r.ApplicationName == applicationName); Role found = results[0]; foreach (string username in usernames) { if (found.EnrolledUsers.Contains(username)) { found.EnrolledUsers.Remove(username); } } dbc.Store(found); } foreach (string username in usernames) { IList <EnrolledUser> results = dbc.Query((EnrolledUser u) => u.Username == username && u.ApplicationName == applicationName); if (results.Count == 0) { continue; } EnrolledUser found = results[0]; foreach (string rolename in rolenames) { if (found.Roles.Contains(rolename)) { found.Roles.Remove(rolename); } } dbc.Store(found); } } }
public override string[] GetRolesForUser(string username) { using (Db4oDatabase dbc = new Db4oDatabase(connectionString)) { IList <EnrolledUser> results = dbc.Query((EnrolledUser u) => u.Username == username && u.ApplicationName == applicationName); if (results.Count == 0) { return(new string[0]); } EnrolledUser found = results[0]; return(found.Roles.ToArray()); } }
public override void AddUsersToRoles(string[] usernames, string[] rolenames) { using (Db4oDatabase dbc = new Db4oDatabase(connectionString)) { foreach (string rolename in rolenames) { if (!RoleExists(rolename, dbc)) { throw new ProviderException("Role name not found."); } } foreach (string username in usernames) { if (username.Contains(",")) { throw new ArgumentException("User names cannot contain commas."); } } foreach (string rolename in rolenames) { IList <Role> results = dbc.Query((Role r) => r.Rolename == rolename && r.ApplicationName == applicationName); Role found = results[0]; foreach (string username in usernames) { if (!found.EnrolledUsers.Contains(username)) { found.EnrolledUsers.Add(username); } } dbc.Store(found); } foreach (string username in usernames) { IList <EnrolledUser> results = dbc.Query((EnrolledUser u) => u.Username == username && u.ApplicationName == applicationName); EnrolledUser found; if (results.Count == 0) { // this is the only place in the API that EnrolledUser's // are added to the database found = new EnrolledUser(username, applicationName); } else { found = results[0]; } foreach (string rolename in rolenames) { if (!found.Roles.Contains(rolename)) { found.Roles.Add(rolename); } } dbc.Store(found); } } }