2
2
{
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . Security . Claims ;
5
6
using System . Web ;
7
+ using APIJSON . NET . Models ;
6
8
using Microsoft . AspNetCore . Mvc ;
9
+ using Microsoft . Extensions . Configuration ;
7
10
using Microsoft . Extensions . Options ;
8
11
using Newtonsoft . Json . Linq ;
9
12
using SqlSugar ;
10
-
13
+ using System . Linq ;
11
14
[ Route ( "api/[controller]" ) ]
12
15
[ ApiController ]
13
16
public class JsonController : ControllerBase
14
17
{
15
- private DbOptions _options ;
16
- private JsonToSql sqlbuilder ;
18
+
19
+ private JsonToSql jsonToSql ;
17
20
private DbContext db ;
18
- public JsonController ( IOptions < DbOptions > options , JsonToSql jsonToSql , DbContext _db )
21
+ protected List < Role > roles ;
22
+ public JsonController ( JsonToSql jsonTo , DbContext _db , IOptions < List < Role > > _roles )
19
23
{
20
- _options = options . Value ;
21
- sqlbuilder = jsonToSql ;
24
+
25
+ jsonToSql = jsonTo ;
22
26
db = _db ;
27
+ roles = _roles . Value ;
23
28
}
24
29
/// <summary>
25
30
/// 查询
@@ -43,21 +48,17 @@ public ActionResult Query([FromBody]string json)
43
48
{
44
49
var htt = new JArray ( ) ;
45
50
var jb = JObject . Parse ( item . Value . ToString ( ) ) ;
46
- int page = jb [ "page" ] == null ? 0 : int . Parse ( jb [ "page" ] . ToString ( ) ) , count = jb [ "count" ] == null ? 0 : int . Parse ( jb [ "count" ] . ToString ( ) )
47
- , query = jb [ "query" ] == null ? 0 : int . Parse ( jb [ "query" ] . ToString ( ) ) ;
48
- jb . Remove ( "page" ) ;
49
- jb . Remove ( "count" ) ;
50
- List < string > tables = new List < string > ( ) ;
51
- List < string > where = new List < string > ( ) ;
51
+ int page = jb [ "page" ] == null ? 0 : int . Parse ( jb [ "page" ] . ToString ( ) ) , count = jb [ "count" ] == null ? 0 : int . Parse ( jb [ "count" ] . ToString ( ) ) , query = jb [ "query" ] == null ? 0 : int . Parse ( jb [ "query" ] . ToString ( ) ) ;
52
+ jb . Remove ( "page" ) ; jb . Remove ( "count" ) ;
53
+ List < string > tables = new List < string > ( ) , where = new List < string > ( ) ;
52
54
foreach ( var t in jb )
53
55
{
54
- tables . Add ( t . Key ) ;
55
- where . Add ( t . Value . ToString ( ) ) ;
56
+ tables . Add ( t . Key ) ; where . Add ( t . Value . ToString ( ) ) ;
56
57
}
57
58
if ( tables . Count > 0 )
58
59
{
59
60
string table = tables [ 0 ] ;
60
- var template = sqlbuilder . GetTableData ( table , page , count , where [ 0 ] , null ) ;
61
+ var template = jsonToSql . GetTableData ( table , page , count , where [ 0 ] , null , User . FindFirstValue ( ClaimTypes . Role ) ) ;
61
62
foreach ( var dd in template )
62
63
{
63
64
var zht = new JObject ( ) ;
@@ -71,21 +72,21 @@ public ActionResult Query([FromBody]string json)
71
72
var jbb = JObject . Parse ( where [ i ] ) ;
72
73
page = jbb [ "page" ] == null ? 0 : int . Parse ( jbb [ "page" ] . ToString ( ) ) ;
73
74
count = jbb [ "count" ] == null ? 0 : int . Parse ( jbb [ "count" ] . ToString ( ) ) ;
74
- template = sqlbuilder . GetTableData ( subtable , page , count , jbb [ subtable ] . ToString ( ) , zht ) ;
75
+
75
76
var lt = new JArray ( ) ;
76
- foreach ( var d in template )
77
+ foreach ( var d in jsonToSql . GetTableData ( subtable , page , count , jbb [ subtable ] . ToString ( ) , zht , User . FindFirstValue ( ClaimTypes . Role ) ) )
77
78
{
78
79
lt . Add ( JToken . FromObject ( d ) ) ;
79
80
}
80
81
zht . Add ( tables [ i ] , lt ) ;
81
82
}
82
83
else
83
84
{
84
- template = sqlbuilder . GetTableData ( subtable , 0 , 0 , where [ i ] . ToString ( ) , zht ) ;
85
+ var ddf = jsonToSql . GetTableData ( subtable , 0 , 0 , where [ i ] . ToString ( ) , zht , User . FindFirstValue ( ClaimTypes . Role ) ) ;
85
86
86
- if ( template != null )
87
+ if ( ddf != null )
87
88
{
88
- zht . Add ( subtable , JToken . FromObject ( template ) ) ;
89
+ zht . Add ( subtable , JToken . FromObject ( ddf ) ) ;
89
90
}
90
91
91
92
}
@@ -97,16 +98,15 @@ public ActionResult Query([FromBody]string json)
97
98
}
98
99
else if ( key . EndsWith ( "[]" ) )
99
100
{
100
-
101
+
101
102
var htt = new JArray ( ) ;
102
103
var jb = JObject . Parse ( item . Value . ToString ( ) ) ;
103
104
int page = jb [ "page" ] == null ? 0 : int . Parse ( jb [ "page" ] . ToString ( ) ) , count = jb [ "count" ] == null ? 0 : int . Parse ( jb [ "count" ] . ToString ( ) ) ;
104
105
jb . Remove ( "page" ) ;
105
106
jb . Remove ( "count" ) ;
106
107
foreach ( var t in jb )
107
108
{
108
- var template = sqlbuilder . GetTableData ( t . Key , page , count , t . Value . ToString ( ) , null ) ;
109
- foreach ( var d in template )
109
+ foreach ( var d in jsonToSql . GetTableData ( t . Key , page , count , t . Value . ToString ( ) , null , User . FindFirstValue ( ClaimTypes . Role ) ) )
110
110
{
111
111
htt . Add ( JToken . FromObject ( d ) ) ;
112
112
}
@@ -115,7 +115,7 @@ public ActionResult Query([FromBody]string json)
115
115
}
116
116
else
117
117
{
118
- var template = sqlbuilder . GetTableData ( key , 0 , 0 , item . Value . ToString ( ) , ht ) ;
118
+ var template = jsonToSql . GetTableData ( key , 0 , 0 , item . Value . ToString ( ) , ht , User . FindFirstValue ( ClaimTypes . Role ) ) ;
119
119
if ( template != null )
120
120
{
121
121
ht . Add ( key , JToken . FromObject ( template ) ) ;
@@ -147,26 +147,29 @@ public ActionResult Add([FromBody]string json)
147
147
{
148
148
JObject jobject = JObject . Parse ( json ) ;
149
149
var sb = new System . Text . StringBuilder ( 100 ) ;
150
-
150
+
151
151
foreach ( var item in jobject )
152
152
{
153
153
string key = item . Key . Trim ( ) ;
154
-
154
+ var role = jsonToSql . GetRole ( User . FindFirstValue ( ClaimTypes . Role ) ) ;
155
+ if ( ! role . Insert . Table . Contains ( key , StringComparer . CurrentCultureIgnoreCase ) )
156
+ {
157
+ ht [ "code" ] = "500" ;
158
+ ht [ "msg" ] = $ "没权限添加{ key } ";
159
+ break ;
160
+ }
155
161
var dt = new Dictionary < string , object > ( ) ;
156
162
foreach ( var f in JObject . Parse ( item . Value . ToString ( ) ) )
157
163
{
158
- dt . Add ( f . Key , f . Value ) ;
164
+ if ( f . Key . ToLower ( ) != "id" && role . Insert . Column . Contains ( f . Key , StringComparer . CurrentCultureIgnoreCase ) )
165
+ dt . Add ( f . Key , f . Value ) ;
159
166
}
160
-
161
167
int id = db . Db . Insertable ( dt ) . AS ( key ) . ExecuteReturnIdentity ( ) ;
162
168
ht . Add ( key , JToken . FromObject ( new { code = 200 , msg = "success" , id } ) ) ;
163
-
164
169
}
165
-
166
170
}
167
171
catch ( Exception ex )
168
172
{
169
-
170
173
ht [ "code" ] = "500" ;
171
174
ht [ "msg" ] = ex . Message ;
172
175
}
@@ -187,22 +190,30 @@ public ActionResult Edit([FromBody]string json)
187
190
try
188
191
{
189
192
JObject jobject = JObject . Parse ( json ) ;
190
-
193
+
191
194
foreach ( var item in jobject )
192
195
{
193
196
string key = item . Key . Trim ( ) ;
197
+ var role = jsonToSql . GetRole ( User . FindFirstValue ( ClaimTypes . Role ) ) ;
198
+ if ( ! role . Update . Table . Contains ( key , StringComparer . CurrentCultureIgnoreCase ) )
199
+ {
200
+ ht [ "code" ] = "500" ;
201
+ ht [ "msg" ] = $ "没权限修改{ key } ";
202
+ break ;
203
+ }
194
204
var value = JObject . Parse ( item . Value . ToString ( ) ) ;
195
205
if ( ! value . ContainsKey ( "id" ) )
196
206
{
197
207
ht [ "code" ] = "500" ;
198
208
ht [ "msg" ] = "未传主键id" ;
199
209
break ;
200
210
}
211
+
201
212
var dt = new Dictionary < string , object > ( ) ;
202
213
dt . Add ( "id" , value [ "id" ] ) ;
203
214
foreach ( var f in value )
204
215
{
205
- if ( f . Key . ToLower ( ) != "id" )
216
+ if ( f . Key . ToLower ( ) != "id" && role . Update . Column . Contains ( f . Key , StringComparer . CurrentCultureIgnoreCase ) )
206
217
{
207
218
dt . Add ( f . Key , f . Value ) ;
208
219
}
@@ -233,14 +244,26 @@ public ActionResult Remove([FromBody]string json)
233
244
ht . Add ( "msg" , "success" ) ;
234
245
try
235
246
{
247
+ var role = jsonToSql . GetRole ( User . FindFirstValue ( ClaimTypes . Role ) ) ;
236
248
JObject jobject = JObject . Parse ( json ) ;
237
-
238
249
foreach ( var item in jobject )
239
250
{
240
251
string key = item . Key . Trim ( ) ;
241
252
var value = JObject . Parse ( item . Value . ToString ( ) ) ;
242
253
var sb = new System . Text . StringBuilder ( 100 ) ;
243
254
sb . Append ( $ "delete [{ key } ] where") ;
255
+ if ( role . Delete == null || role . Delete . Table == null )
256
+ {
257
+ ht [ "code" ] = "500" ;
258
+ ht [ "msg" ] = "delete权限未配置" ;
259
+ break ;
260
+ }
261
+ if ( ! role . Delete . Table . Contains ( key , StringComparer . CurrentCultureIgnoreCase ) )
262
+ {
263
+ ht [ "code" ] = "500" ;
264
+ ht [ "msg" ] = $ "没权限删除{ key } ";
265
+ break ;
266
+ }
244
267
if ( ! value . ContainsKey ( "id" ) )
245
268
{
246
269
ht [ "code" ] = "500" ;
@@ -251,14 +274,12 @@ public ActionResult Remove([FromBody]string json)
251
274
foreach ( var f in value )
252
275
{
253
276
sb . Append ( $ "{ f . Key } =@{ f . Key } ,") ;
254
-
255
277
p . Add ( new SugarParameter ( $ "@{ f . Key } ", f . Value . ToString ( ) ) ) ;
256
278
}
257
-
258
279
string sql = sb . ToString ( ) . TrimEnd ( ',' ) ;
259
280
db . Db . Ado . ExecuteCommand ( sql , p ) ;
260
281
ht . Add ( key , JToken . FromObject ( new { code = 200 , msg = "success" , id = value [ "id" ] . ToString ( ) } ) ) ;
261
-
282
+
262
283
}
263
284
}
264
285
catch ( Exception ex )
0 commit comments