2016-10-22 20:55:07 +08:00
|
|
|
|
using Bootstrap.Admin.Models;
|
|
|
|
|
using Bootstrap.DataAccess;
|
2016-11-16 20:06:28 +08:00
|
|
|
|
using Longbow.Security.Principal;
|
2016-10-28 20:18:12 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2016-11-01 08:10:59 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Http;
|
2016-10-22 20:55:07 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.Admin.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UsersController : ApiController
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
2016-10-24 20:53:05 +08:00
|
|
|
|
public QueryData<User> Get([FromUri]QueryUserOption value)
|
2016-10-22 20:55:07 +08:00
|
|
|
|
{
|
2016-10-24 20:53:05 +08:00
|
|
|
|
return value.RetrieveData();
|
2016-10-22 20:55:07 +08:00
|
|
|
|
}
|
2016-10-28 20:18:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-11-16 20:06:28 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPut]
|
|
|
|
|
public bool Put([FromBody]User value)
|
|
|
|
|
{
|
2016-11-27 14:21:21 +08:00
|
|
|
|
if (value.UserStatus == 9)
|
|
|
|
|
{
|
|
|
|
|
// vlaidate userName
|
|
|
|
|
return UserHelper.RetrieveUsersByName(value.UserName) == null;
|
|
|
|
|
}
|
2016-11-16 20:06:28 +08:00
|
|
|
|
var ret = false;
|
|
|
|
|
var userName = User.Identity.Name;
|
|
|
|
|
if (value.UserName == userName && !LgbPrincipal.IsAdmin(userName))
|
|
|
|
|
{
|
|
|
|
|
if (value.UserStatus == 1)
|
|
|
|
|
ret = UserHelper.SaveUserInfoByName(value);
|
|
|
|
|
else if (value.UserStatus == 2)
|
|
|
|
|
ret = UserHelper.ChangePassword(value);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-10-28 20:18:12 +08:00
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IEnumerable<User> Post(int id, [FromBody]JObject value)
|
|
|
|
|
{
|
|
|
|
|
var ret = new List<User>();
|
|
|
|
|
dynamic json = value;
|
|
|
|
|
switch ((string)json.type)
|
|
|
|
|
{
|
|
|
|
|
case "role":
|
|
|
|
|
ret = UserHelper.RetrieveUsersByRoleId(id).ToList();
|
|
|
|
|
break;
|
2016-10-29 17:38:23 +08:00
|
|
|
|
case "group":
|
|
|
|
|
ret = UserHelper.RetrieveUsersByGroupId(id).ToList();
|
|
|
|
|
break;
|
2016-10-28 20:18:12 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-10-22 20:55:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
2016-11-27 14:21:21 +08:00
|
|
|
|
public User Get(string userName)
|
2016-10-22 20:55:07 +08:00
|
|
|
|
{
|
2016-11-27 14:21:21 +08:00
|
|
|
|
return UserHelper.RetrieveUsersByName(userName);
|
2016-10-22 20:55:07 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public bool Post([FromBody]User value)
|
|
|
|
|
{
|
2016-11-10 12:20:26 +08:00
|
|
|
|
value.Description = string.Format("管理员{0}创建用户", User.Identity.Name);
|
2016-11-11 14:47:54 +08:00
|
|
|
|
value.ApprovedBy = User.Identity.Name;
|
2016-10-22 20:55:07 +08:00
|
|
|
|
return UserHelper.SaveUser(value);
|
|
|
|
|
}
|
2016-11-18 10:18:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-10-28 20:18:12 +08:00
|
|
|
|
[HttpPut]
|
|
|
|
|
public bool Put(int id, [FromBody]JObject value)
|
|
|
|
|
{
|
|
|
|
|
var ret = false;
|
|
|
|
|
dynamic json = value;
|
|
|
|
|
string userIds = json.userIds;
|
|
|
|
|
switch ((string)json.type)
|
|
|
|
|
{
|
|
|
|
|
case "role":
|
|
|
|
|
ret = UserHelper.SaveUsersByRoleId(id, userIds);
|
|
|
|
|
break;
|
2016-10-29 17:38:23 +08:00
|
|
|
|
case "group":
|
|
|
|
|
ret = UserHelper.SaveUsersByGroupId(id, userIds);
|
|
|
|
|
break;
|
2016-11-12 15:26:40 +08:00
|
|
|
|
case "user":
|
|
|
|
|
// 此时 userIds 存储的信息是操作结果 1 标示同意 0 标示拒绝
|
|
|
|
|
var user = new User() { ID = id, UserStatus = 2 };
|
|
|
|
|
if (userIds == "1")
|
|
|
|
|
{
|
|
|
|
|
user.ApprovedBy = User.Identity.Name;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
user.RejectedReason = "无原因";
|
|
|
|
|
user.RejectedBy = User.Identity.Name;
|
|
|
|
|
}
|
|
|
|
|
ret = UserHelper.SaveUser(user);
|
|
|
|
|
break;
|
2016-10-28 20:18:12 +08:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-10-22 20:55:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public bool Delete([FromBody]string value)
|
|
|
|
|
{
|
|
|
|
|
return UserHelper.DeleteUser(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-20 17:55:29 +08:00
|
|
|
|
}
|