zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Bootstrap.Admin.Query;
using Bootstrap.DataAccess;
using Prow.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace Bootstrap.Admin.Controllers.Api
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class UsersController : ControllerBase
    {
        /// <summary>
        /// 调用获取所有用户信息 用户管理查询按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet]
        public QueryData<object> Get([FromQuery]QueryUserOption value)
        {
            return value.RetrieveData();
        }
 
        /// <summary>
        /// 用户相关授权操作
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="type">类型 如角色、部门</param>
        /// <returns></returns>
        [HttpPost("{id}")]
        public IEnumerable<object> Post(string id, [FromQuery]string type) => type switch
        {
            "role" => UserHelper.RetrievesByRoleId(id).Select(p => new
            {
                p.Id,
                p.DisplayName,
                p.UserName,
                p.Checked
            }).OrderBy(u => u.DisplayName),
            "group" => UserHelper.RetrievesByGroupId(id),
            "reset" => UserHelper.RetrieveResetReasonsByUserName(id).Select(u => new { u.Key, u.Value }),
            _ => new string[0]
        };
 
        /// <summary>
        /// 前台User View调用,新建/更新用户
        /// </summary>
        /// <param name="value"></param>
        [HttpPost]
        [ButtonAuthorize(Url = "~/Admin/Users", Auth = "add,edit")]
        public bool Post([FromBody]User value)
        {
            bool ret;
            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;
        }
 
        /// <summary>
        /// 保存授权操作
        /// </summary>
        /// <param name="id"></param>
        /// <param name="values"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ButtonAuthorize(Url = "~/Admin/Users", Auth = "assignRole,assignGroup")]
        public bool Put(string id, [FromBody]IEnumerable<string> values, [FromQuery]string type) => type switch
        {
            "role" => RoleHelper.SaveByUserId(id, values),
            "group" => GroupHelper.SaveByUserId(id, values),
            _ => false
        };
 
        /// <summary>
        /// 删除用户操作
        /// </summary>
        /// <param name="value"></param>
        [HttpDelete]
        [ButtonAuthorize(Url = "~/Admin/Users", Auth = "del")]
        public bool Delete([FromBody]IEnumerable<string> value)
        {
            return UserHelper.Delete(value);
        }
 
        /// <summary>
        /// api 握手协议
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpOptions]
        public string? Options()
        {
            return null;
        }
    }
}