using Bootstrap.Admin.Query; using Bootstrap.DataAccess; using Longbow.Web.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; namespace Bootstrap.Admin.Controllers.Api { /// /// /// [Route("api/[controller]")] [ApiController] public class UsersController : ControllerBase { /// /// /// /// /// [HttpGet] public QueryData Get([FromQuery]QueryUserOption value) { return value.RetrieveData(); } /// /// /// /// [HttpPut] public bool Put([FromBody]User value) { if (User.IsInRole("Administrators")) return false; var ret = false; if (value.UserStatus == UserStates.ChangeTheme) { return UserHelper.SaveUserCssByName(value.UserName, value.Css); } if (value.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase)) { if (value.UserStatus == UserStates.ChangeDisplayName) ret = UserHelper.SaveDisplayName(value.UserName, value.DisplayName); else if (value.UserStatus == UserStates.ChangePassword) ret = UserHelper.ChangePassword(value.UserName, value.Password, value.NewPassword); else if (value.UserStatus == UserStates.SaveApp) ret = UserHelper.SaveApp(value.UserName, value.App); } return ret; } /// /// /// /// /// /// [HttpPost("{id}")] public IEnumerable Post(string id, [FromQuery]string type) { IEnumerable ret = null; switch (type) { case "role": ret = UserHelper.RetrievesByRoleId(id).Select(p => new { p.Id, p.DisplayName, p.UserName, p.Checked }); break; case "group": ret = UserHelper.RetrievesByGroupId(id); break; case "reset": ret = UserHelper.RetrieveResetReasonsByUserName(id).Select(u => new { u.Key, u.Value }); break; } return ret; } /// /// 前台User View调用,新建/更新用户 /// /// [HttpPost] public bool Post([FromBody]User value) { var ret = false; if (string.IsNullOrEmpty(value.Id)) { value.Description = string.Format("管理员{0}创建用户", User.Identity.Name); value.ApprovedBy = User.Identity.Name; value.ApprovedTime = DateTime.Now; ret = UserHelper.Save(value); } else { ret = UserHelper.Update(value.Id, value.Password, value.DisplayName); } return ret; } /// /// /// /// /// /// /// [HttpPut("{id}")] public bool Put(string id, [FromBody]IEnumerable userIds, [FromQuery]string type) { var ret = false; switch (type) { case "role": ret = UserHelper.SaveByRoleId(id, userIds); break; case "group": ret = UserHelper.SaveByGroupId(id, userIds); break; } return ret; } /// /// /// /// [HttpDelete] public bool Delete([FromBody]IEnumerable value) { return UserHelper.Delete(value); } /// /// /// /// [AllowAnonymous] [HttpOptions] public string Options() { return null; } } }