diff --git a/Bootstrap.Admin/Controllers/Api/UsersController.cs b/Bootstrap.Admin/Controllers/Api/UsersController.cs index 0367244b..e22b6bc2 100644 --- a/Bootstrap.Admin/Controllers/Api/UsersController.cs +++ b/Bootstrap.Admin/Controllers/Api/UsersController.cs @@ -54,21 +54,23 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPost("{id}")] - public IEnumerable Post(string id, [FromQuery]string type) + public IEnumerable Post(string id, [FromQuery]string type) { - var ret = new List(); 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; } /// /// 前台User View调用,新建/更新用户 diff --git a/Bootstrap.DataAccess.MongoDB/User.cs b/Bootstrap.DataAccess.MongoDB/User.cs index 3e1cd7ef..5cf27506 100644 --- a/Bootstrap.DataAccess.MongoDB/User.cs +++ b/Bootstrap.DataAccess.MongoDB/User.cs @@ -21,6 +21,7 @@ namespace Bootstrap.DataAccess.MongoDB /// /// public IEnumerable Groups { get; set; } + /// /// /// @@ -35,6 +36,7 @@ namespace Bootstrap.DataAccess.MongoDB .Include(u => u.Css); return MongoDbAccessManager.Users.Find(user => user.UserName == userName).Project(project).FirstOrDefault(); } + /// /// /// @@ -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); } + /// /// /// /// public override IEnumerable RetrieveNewUsers() => MongoDbAccessManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList(); + /// /// /// @@ -70,6 +74,7 @@ namespace Bootstrap.DataAccess.MongoDB .Include(u => u.Roles); return MongoDbAccessManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project(project).ToList(); } + /// /// /// @@ -91,6 +96,7 @@ namespace Bootstrap.DataAccess.MongoDB MongoDbAccessManager.Users.InsertOne(user as User); return true; } + /// /// /// @@ -106,6 +112,7 @@ namespace Bootstrap.DataAccess.MongoDB MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == id, update); return true; } + /// /// /// @@ -125,6 +132,7 @@ namespace Bootstrap.DataAccess.MongoDB } return ret; } + /// /// /// @@ -140,5 +148,46 @@ namespace Bootstrap.DataAccess.MongoDB MongoDbAccessManager.Users.BulkWrite(list); return true; } + + /// + /// + /// + /// + /// + public override IEnumerable RetrieveUsersByRoleId(string roleId) + { + var users = UserHelper.RetrieveUsers().Cast().ToList(); + users.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : ""); + return users; + } + + /// + /// + /// + /// + /// + /// + public override bool SaveUsersByRoleId(string roleId, IEnumerable 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() : p.Roles.ToList(); + roles.Remove(roleId); + MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders.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() : p.Roles.ToList(); + roles.Add(roleId); + MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Roles, roles)); + }); + return true; + } } }