增加功能:完善角色管理对用户授权
This commit is contained in:
parent
0861d4636b
commit
5661df8535
|
@ -54,21 +54,23 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("{id}")]
|
[HttpPost("{id}")]
|
||||||
public IEnumerable<User> Post(string id, [FromQuery]string type)
|
public IEnumerable<object> Post(string id, [FromQuery]string type)
|
||||||
{
|
{
|
||||||
var ret = new List<User>();
|
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case "role":
|
case "role":
|
||||||
ret = UserHelper.RetrieveUsersByRoleId(id).ToList();
|
return UserHelper.RetrieveUsersByRoleId(id).Select(p => new
|
||||||
break;
|
{
|
||||||
|
p.Id,
|
||||||
|
p.DisplayName,
|
||||||
|
p.UserName,
|
||||||
|
p.Checked
|
||||||
|
});
|
||||||
case "group":
|
case "group":
|
||||||
ret = UserHelper.RetrieveUsersByGroupId(id).ToList();
|
return UserHelper.RetrieveUsersByGroupId(id).ToList();
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
return null;
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 前台User View调用,新建/更新用户
|
/// 前台User View调用,新建/更新用户
|
||||||
|
|
|
@ -21,6 +21,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<string> Groups { get; set; }
|
public IEnumerable<string> Groups { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -35,6 +36,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
.Include(u => u.Css);
|
.Include(u => u.Css);
|
||||||
return MongoDbAccessManager.Users.Find(user => user.UserName == userName).Project<DataAccess.User>(project).FirstOrDefault();
|
return MongoDbAccessManager.Users.Find(user => user.UserName == userName).Project<DataAccess.User>(project).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -48,11 +50,13 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
var u = MongoDbAccessManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
|
var u = MongoDbAccessManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
|
||||||
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
|
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.User> RetrieveNewUsers() => MongoDbAccessManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
|
public override IEnumerable<DataAccess.User> RetrieveNewUsers() => MongoDbAccessManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -70,6 +74,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
.Include(u => u.Roles);
|
.Include(u => u.Roles);
|
||||||
return MongoDbAccessManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
|
return MongoDbAccessManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -91,6 +96,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
MongoDbAccessManager.Users.InsertOne(user as User);
|
MongoDbAccessManager.Users.InsertOne(user as User);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -106,6 +112,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == id, update);
|
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == id, update);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -125,6 +132,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -140,5 +148,46 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
MongoDbAccessManager.Users.BulkWrite(list);
|
MongoDbAccessManager.Users.BulkWrite(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="roleId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override IEnumerable<DataAccess.User> RetrieveUsersByRoleId(string roleId)
|
||||||
|
{
|
||||||
|
var users = UserHelper.RetrieveUsers().Cast<User>().ToList();
|
||||||
|
users.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : "");
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="roleId"></param>
|
||||||
|
/// <param name="userIds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override bool SaveUsersByRoleId(string roleId, IEnumerable<string> userIds)
|
||||||
|
{
|
||||||
|
var users = MongoDbAccessManager.Users.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
||||||
|
|
||||||
|
// Remove roles
|
||||||
|
users.ForEach(p =>
|
||||||
|
{
|
||||||
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
||||||
|
roles.Remove(roleId);
|
||||||
|
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
||||||
|
});
|
||||||
|
|
||||||
|
users = MongoDbAccessManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
||||||
|
// Add roles
|
||||||
|
users.ForEach(p =>
|
||||||
|
{
|
||||||
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
||||||
|
roles.Add(roleId);
|
||||||
|
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue