public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { SecUtility.CheckArrayParameter(ref usernames, true, true, InvalidUsernameCharacters, MAX_USERNAME_LENGTH, "usernames"); SecUtility.CheckArrayParameter(ref roleNames, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, "roleNames"); // update all users' roles var roles = new List <string>(); foreach (var role in roleNames) { roles.Add(role.ToLowerInvariant()); } var users = new List <string>(); foreach (var username in usernames) { users.Add(username.ToLowerInvariant()); } var query = Query.In(ElementNames.LowercaseUsername, new BsonArray(users.ToArray())); var update = Update.PullAllWrapped <string>(ElementNames.Roles, roles); var result = _db.GetCollection <User>(_userCollectionName).Update(query, update, UpdateFlags.Multi, SafeMode.True); if (result.HasLastErrorMessage) { throw new ProviderException(result.LastErrorMessage); } }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { SecUtility.CheckParameter(ref roleName, true, true, null, MAX_ROLE_LENGTH, "roleName"); if (String.IsNullOrWhiteSpace(usernameToMatch)) { return(new string[0]); } var username = ElementNames.LowercaseUsername; QueryComplete userQuery = Helper.FindQuery(usernameToMatch.ToLowerInvariant(), username); var query = Query.And( Query.EQ(ElementNames.Roles, roleName.ToLowerInvariant()), userQuery ); var cursor = UserCollection.FindAs <BsonDocument>(query); // only want the usernames cursor.SetFields(Fields.Include(username).Exclude("_id")); var names = new List <string>(); foreach (var doc in cursor) { var str = doc[username].AsString; names.Add(str); } return(names.ToArray()); }
public override void AddUsersToRoles(string[] usernames, string[] roleNames) { SecUtility.CheckArrayParameter(ref usernames, true, true, InvalidUsernameCharacters, MAX_USERNAME_LENGTH, USER_NAMES); SecUtility.CheckArrayParameter(ref roleNames, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAMES); // ensure lowercase var roles = roleNames.Select(role => role.ToLowerInvariant()).ToList(); var users = usernames.Select(username => username.ToLowerInvariant()).ToList(); // first add any non-existant roles to roles collection // a) pull all roles, filter out existing, push new // ...or // b) save all passed in roles foreach (var role in roles) { CreateRole(role); } // now update all users' roles var query = Query.In(ElementNames.LowercaseUsername, new BsonArray(users.ToArray())); var update = Update.AddToSetEachWrapped <string>(ElementNames.Roles, roles); var result = UserCollection.Update(query, update, UpdateFlags.Multi, SafeMode.True); if (result.HasLastErrorMessage) { throw new ProviderException(result.LastErrorMessage); } }
public override bool IsUserInRole(string username, string roleName) { SecUtility.CheckParameter(ref roleName, true, true, null, MAX_ROLE_LENGTH, ROLE_NAME); var found = UserCollection.AsQueryable().Any(u => u.LowercaseUsername == username.ToLowerInvariant() && u.Roles.Contains(roleName.ToLowerInvariant())); return(found); }
public override string[] GetUsersInRole(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAME); var usernames = UserCollection.AsQueryable() .Where(u => u.Roles.Contains(roleName.ToLowerInvariant())) .Select(u => u.Username).ToArray(); return(usernames); }
public override string[] GetRolesForUser(string username) { SecUtility.CheckParameter(ref username, true, true, InvalidUsernameCharacters, MAX_USERNAME_LENGTH, "username"); string[] roles = UserCollection.AsQueryable() .Where(u => u.LowercaseUsername == username.ToLowerInvariant()) .Select(u => u.Roles.ToArray()).FirstOrDefault(); return(roles); }
public override void CreateRole(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAME); var doc = new BsonDocument(); doc.Set(DOCUMENT_ID_NAME, roleName.ToLowerInvariant()); RoleCollection.ReplaceOne(Builders <BsonDocument> .Filter.Eq("_id", roleName.ToLowerInvariant()), doc, new UpdateOptions { IsUpsert = true }); }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { SecUtility.CheckArrayParameter(ref usernames, true, true, InvalidUsernameCharacters, MAX_USERNAME_LENGTH, USER_NAMES); SecUtility.CheckArrayParameter(ref roleNames, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAMES); // update all users' roles var roles = roleNames.Select(role => role.ToLowerInvariant()).ToList(); var users = usernames.Select(username => username.ToLowerInvariant()).ToList(); var query = Builders <User> .Filter.In(ElementNames.LowercaseUsername, new BsonArray(users.ToArray())); var update = Builders <User> .Update.PullAll <string>(ElementNames.Roles, roles); var result = UserCollection.UpdateMany(query, update); }
public override void CreateRole(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, "roleName"); var doc = new BsonDocument(); doc.SetDocumentId(roleName.ToLowerInvariant()); var result = RoleCollection.Save(doc, SafeMode.True); if (!result.Ok) { throw new ProviderException(String.Format("Could not create role '{0}'. Reason: {1}", roleName, result.LastErrorMessage)); } }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { SecUtility.CheckParameter(ref roleName, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAME); var rolePopulated = UserCollection.AsQueryable().Any(u => u.Roles.Contains(roleName.ToLowerInvariant())); if (throwOnPopulatedRole && rolePopulated) { throw new ProviderException(Resources.Role_is_not_empty); } var result = RoleCollection.DeleteOne(Builders <BsonDocument> .Filter.Eq(DOCUMENT_ID_NAME, roleName.ToLowerInvariant())); return(result.IsAcknowledged); }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { SecUtility.CheckParameter(ref roleName, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, "roleName"); var rolePopulated = UserCollection.AsQueryable().Any(u => u.Roles.Contains(roleName.ToLowerInvariant())); if (throwOnPopulatedRole && rolePopulated) { throw new ProviderException(Resources.Role_is_not_empty); } var result = RoleCollection.Remove(Query.EQ("_id", roleName.ToLowerInvariant()), SafeMode.True); return(result.Ok); }
public override void CreateRole(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAME); var doc = new BsonDocument(); doc.Set(DOCUMENT_ID_NAME, roleName.ToLowerInvariant()); var result = RoleCollection.Save(doc, WriteConcern.Acknowledged); if (!result.Ok) { throw new ProviderException(String.Format("Could not create role '{0}'. Reason: {1}", roleName, result.LastErrorMessage)); } }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { SecUtility.CheckParameter(ref roleName, true, true, null, MAX_ROLE_LENGTH, ROLE_NAME); if (String.IsNullOrWhiteSpace(usernameToMatch)) { return(new string[0]); } var username = ElementNames.LowercaseUsername; var userQuery = Helper.FindQuery(usernameToMatch.ToLowerInvariant(), username); var query = Builders <User> .Filter.And( Builders <User> .Filter.Eq(ElementNames.Roles, roleName.ToLowerInvariant()), userQuery.ToBsonDocument() ); var cursor = UserCollection.FindSync <User>(query); return(cursor.ToEnumerable().Select(doc => doc.Username).ToArray()); }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { SecUtility.CheckParameter(ref roleName, true, true, null, MAX_ROLE_LENGTH, ROLE_NAME); if (String.IsNullOrWhiteSpace(usernameToMatch)) { return(new string[0]); } var username = ElementNames.LowercaseUsername; var userQuery = Helper.FindQuery(usernameToMatch.ToLowerInvariant(), username); var query = Query.And( Query.EQ(ElementNames.Roles, roleName.ToLowerInvariant()), userQuery ); var cursor = UserCollection.FindAs <BsonDocument>(query); // only want the usernames cursor.SetFields(Fields.Include(username).Exclude(DOCUMENT_ID_NAME)); return(cursor.Select(doc => doc[username].AsString).ToArray()); }
public override bool RoleExists(string roleName) { SecUtility.CheckParameter(ref roleName, true, true, null, MAX_ROLE_LENGTH, ROLE_NAME); return(Roles.Contains(roleName.ToLowerInvariant())); }