BootstrapAdmin/Bootstrap.Admin/Controllers/Api/UsersController.cs

130 lines
3.8 KiB
C#
Raw Normal View History

using Bootstrap.Admin.Query;
2018-06-07 00:45:47 +08:00
using Bootstrap.DataAccess;
using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
2018-06-07 00:45:47 +08:00
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
2018-06-07 00:45:47 +08:00
{
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public QueryData<object> Get([FromQuery]QueryUserOption value)
2018-06-07 00:45:47 +08:00
{
return value.RetrieveData();
}
2018-06-07 00:45:47 +08:00
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
2018-06-07 00:45:47 +08:00
/// <returns></returns>
[HttpPost("{id}")]
public IEnumerable<object> Post(string id, [FromQuery]string type)
2018-06-07 00:45:47 +08:00
{
IEnumerable<object> ret = null;
switch (type)
2018-06-07 00:45:47 +08:00
{
case "role":
ret = UserHelper.RetrievesByRoleId(id).Select(p => new
{
p.Id,
p.DisplayName,
p.UserName,
p.Checked
});
break;
2018-06-07 00:45:47 +08:00
case "group":
ret = UserHelper.RetrievesByGroupId(id);
break;
case "reset":
ret = UserHelper.RetrieveResetReasonsByUserName(id).Select(u => new { u.Key, u.Value });
break;
2018-06-07 00:45:47 +08:00
}
return ret;
2018-06-07 00:45:47 +08:00
}
2018-06-07 00:45:47 +08:00
/// <summary>
/// 前台User View调用新建/更新用户
2018-06-07 00:45:47 +08:00
/// </summary>
/// <param name="value"></param>
[HttpPost]
[ButtonAuthorize(Url = "~/Admin/Users", Auth = "add,edit")]
2018-06-07 12:25:03 +08:00
public bool Post([FromBody]User value)
2018-06-07 00:45:47 +08:00
{
var ret = false;
2018-10-30 13:07:29 +08:00
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;
2018-06-07 00:45:47 +08:00
}
2018-06-07 00:45:47 +08:00
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="values"></param>
/// <param name="type"></param>
2018-06-07 00:45:47 +08:00
/// <returns></returns>
[HttpPut("{id}")]
[ButtonAuthorize(Url = "~/Admin/Users", Auth = "assignRole,assignGroup")]
public bool Put(string id, [FromBody]IEnumerable<string> values, [FromQuery]string type)
2018-06-07 00:45:47 +08:00
{
var ret = false;
switch (type)
2018-06-07 00:45:47 +08:00
{
case "role":
ret = RoleHelper.SaveByUserId(id, values);
2018-06-07 00:45:47 +08:00
break;
case "group":
ret = GroupHelper.SaveByUserId(id, values);
2018-06-07 00:45:47 +08:00
break;
}
return ret;
}
2018-06-07 00:45:47 +08:00
/// <summary>
///
/// </summary>
/// <param name="value"></param>
[HttpDelete]
[ButtonAuthorize(Url = "~/Admin/Users", Auth = "del")]
2018-10-30 13:07:29 +08:00
public bool Delete([FromBody]IEnumerable<string> value)
2018-06-07 00:45:47 +08:00
{
return UserHelper.Delete(value);
2018-06-07 00:45:47 +08:00
}
2018-06-07 00:45:47 +08:00
/// <summary>
///
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpOptions]
public string Options()
{
return null;
}
}
2016-10-20 17:55:29 +08:00
}