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