2016-08-19 20:45:52 +08:00
|
|
|
|
using System;
|
2015-08-05 04:07:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
2016-08-19 20:45:52 +08:00
|
|
|
|
using System.Collections.Specialized;
|
2015-08-05 04:07:31 +08:00
|
|
|
|
using System.ComponentModel;
|
2016-08-19 20:45:52 +08:00
|
|
|
|
using System.Configuration.Provider;
|
2015-08-05 04:07:31 +08:00
|
|
|
|
using System.Linq;
|
2016-08-19 20:45:52 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2015-08-05 04:07:31 +08:00
|
|
|
|
using System.Web.Security;
|
2016-08-19 20:45:52 +08:00
|
|
|
|
using MongoAuth.Properties;
|
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
|
using MongoDB.Driver;
|
2015-08-05 04:07:31 +08:00
|
|
|
|
|
|
|
|
|
namespace MongoAuth
|
|
|
|
|
{
|
2017-04-12 22:04:23 +08:00
|
|
|
|
public class MongoRoleProvider : RoleProvider
|
|
|
|
|
{
|
|
|
|
|
private const string MEMBER_COLLECTION_NAME = "members";
|
|
|
|
|
private const string ROLE_COLLECTION_NAME = "roles";
|
|
|
|
|
private IMongoCollection<MongoMember> _memberCollection;
|
|
|
|
|
private IMongoCollection<MongoRole> _roleCollection;
|
|
|
|
|
|
|
|
|
|
public override string ApplicationName
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
get => "";
|
2017-04-12 22:04:23 +08:00
|
|
|
|
set { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Initialize(string name, NameValueCollection config)
|
|
|
|
|
{
|
|
|
|
|
base.Initialize(name, config);
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var settings = new MongoClientSettings
|
2016-10-04 06:06:40 +08:00
|
|
|
|
{
|
2017-04-12 22:04:23 +08:00
|
|
|
|
Server = new MongoServerAddress(DatabaseConfig.Host, DatabaseConfig.Port)
|
2016-10-04 06:06:40 +08:00
|
|
|
|
};
|
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(DatabaseConfig.Username) && !string.IsNullOrEmpty(DatabaseConfig.Password))
|
|
|
|
|
{
|
2018-01-06 03:40:12 +08:00
|
|
|
|
settings.Credential = MongoCredential.CreateCredential(DatabaseConfig.Database,
|
|
|
|
|
DatabaseConfig.Username,
|
|
|
|
|
DatabaseConfig.Password);
|
2017-04-12 22:04:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var dbClient = new MongoClient(settings);
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
_roleCollection = dbClient.GetDatabase(DatabaseConfig.Database)
|
|
|
|
|
.GetCollection<MongoRole>(ROLE_COLLECTION_NAME);
|
|
|
|
|
_memberCollection = dbClient.GetDatabase(DatabaseConfig.Database)
|
|
|
|
|
.GetCollection<MongoMember>(MEMBER_COLLECTION_NAME);
|
2017-04-12 22:04:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var roleTask = _roleCollection.Find(r => roleNames.Contains(r.RoleName)).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
roleTask.Wait();
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var roles = roleTask.Result;
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var userTask = _memberCollection.Find(u => usernames.Contains(u.UserName)).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
userTask.Wait();
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var users = userTask.Result;
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < roles.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var newUsers = new List<Guid>();
|
|
|
|
|
|
|
|
|
|
if (roles[i].Users != null)
|
|
|
|
|
{
|
|
|
|
|
newUsers.AddRange(roles[i].Users);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var usersToAdd = from u in users
|
|
|
|
|
where newUsers.All(v => v != u.Id)
|
|
|
|
|
select u.Id;
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
|
|
|
|
newUsers.AddRange(usersToAdd);
|
|
|
|
|
|
|
|
|
|
roles[i].Users = newUsers.ToArray();
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var update =
|
|
|
|
|
_roleCollection.ReplaceOneAsync(Builders<MongoRole>.Filter.Eq(r => r.Id, roles[i].Id), roles[i]);
|
2017-04-12 22:04:23 +08:00
|
|
|
|
update.Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void CreateRole(string roleName)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var role = _roleCollection.Find(r => r.RoleName == roleName).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
role.Wait();
|
|
|
|
|
if (role.Result != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var mr = new MongoRole
|
2017-04-12 22:04:23 +08:00
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
RoleName = roleName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Task task = _roleCollection.InsertOneAsync(mr);
|
|
|
|
|
task.Wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var role = _roleCollection.Find(r => r.RoleName == roleName).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
role.Wait();
|
|
|
|
|
|
|
|
|
|
if (role.Result != null
|
|
|
|
|
&& role.Result.Users.Length > 0
|
|
|
|
|
&& throwOnPopulatedRole)
|
|
|
|
|
{
|
|
|
|
|
throw new ProviderException(Resources.RoleNotEmpty);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var task = _roleCollection.DeleteOneAsync(r => r.RoleName == roleName);
|
2017-04-12 22:04:23 +08:00
|
|
|
|
task.Wait();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-10-04 06:06:40 +08:00
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var role = _roleCollection.Find(r => r.RoleName == roleName).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
role.Wait();
|
2016-08-19 20:45:52 +08:00
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
if (role.Result == null)
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var users = _memberCollection.Find(u
|
|
|
|
|
=> role.Result.Users.Contains(u.Id) && u.UserName.ToLower().Contains(usernameToMatch.ToLower()))
|
|
|
|
|
.ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
users.Wait();
|
|
|
|
|
|
|
|
|
|
return users.Result.Select(r => r.UserName).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string[] GetAllRoles()
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var roles = _roleCollection.Find(new BsonDocument()).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
roles.Wait();
|
|
|
|
|
|
|
|
|
|
return roles.Result.Select(r => r.RoleName).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string[] GetRolesForUser(string username)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var user = _memberCollection.Find(u => u.UserName.ToLower() == username.ToLower()).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
user.Wait();
|
2016-08-19 20:45:52 +08:00
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
if (user.Result == null)
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var role = _roleCollection.Find(r => r.Users != null && r.Users.Contains(user.Result.Id)).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
role.Wait();
|
|
|
|
|
|
|
|
|
|
return role.Result.Select(r => r.RoleName).ToArray();
|
|
|
|
|
}
|
2016-08-19 20:45:52 +08:00
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
public override string[] GetUsersInRole(string roleName)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var role = _roleCollection.Find(r => r.RoleName == roleName).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
role.Wait();
|
2016-08-19 20:45:52 +08:00
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
if (role.Result == null)
|
2015-08-05 04:07:31 +08:00
|
|
|
|
{
|
2017-04-12 22:04:23 +08:00
|
|
|
|
return Array.Empty<string>();
|
2015-08-05 04:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var users = _memberCollection.Find(u => role.Result.Users.Contains(u.Id)).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
users.Wait();
|
|
|
|
|
|
|
|
|
|
return users.Result.Select(u => u.UserName).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool IsUserInRole(string username, string roleName)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var user = _memberCollection.Find(u => u.UserName.ToLower() == username.ToLower()).SingleOrDefaultAsync();
|
|
|
|
|
var role = _roleCollection.Find(r => r.RoleName == roleName).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
user.Wait();
|
|
|
|
|
role.Wait();
|
|
|
|
|
|
|
|
|
|
if (user.Result == null
|
|
|
|
|
|| role.Result?.Users == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return role.Result.Users.Contains(user.Result.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var roleTask = _roleCollection.Find(r => roleNames.Contains(r.RoleName)).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
roleTask.Wait();
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var roles = roleTask.Result;
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var userTask = _memberCollection.Find(u => usernames.Contains(u.UserName)).ToListAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
userTask.Wait();
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var users = userTask.Result;
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
|
|
|
|
foreach (MongoRole t in roles)
|
|
|
|
|
{
|
|
|
|
|
t.Users = (from u in t.Users
|
2018-02-07 06:16:37 +08:00
|
|
|
|
where users.All(v => v.Id != u)
|
|
|
|
|
select u).ToArray();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var update = _roleCollection.ReplaceOneAsync(Builders<MongoRole>.Filter.Eq(r => r.Id, t.Id), t);
|
2017-04-12 22:04:23 +08:00
|
|
|
|
update.Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool RoleExists(string roleName)
|
|
|
|
|
{
|
2018-02-07 06:16:37 +08:00
|
|
|
|
var role = _roleCollection.Find(r => r.RoleName == roleName).SingleOrDefaultAsync();
|
2017-04-12 22:04:23 +08:00
|
|
|
|
role.Wait();
|
|
|
|
|
|
|
|
|
|
return role.Result != null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DataObject]
|
|
|
|
|
public class MongoRole
|
|
|
|
|
{
|
|
|
|
|
[BsonId]
|
|
|
|
|
public Guid Id { get; set; }
|
|
|
|
|
|
|
|
|
|
public string RoleName { get; set; }
|
2015-08-05 04:07:31 +08:00
|
|
|
|
|
2017-04-12 22:04:23 +08:00
|
|
|
|
public Guid[] Users { get; set; }
|
|
|
|
|
}
|
2015-08-05 04:07:31 +08:00
|
|
|
|
}
|