1
1
using Microsoft . AspNetCore . Mvc ;
2
2
using Microsoft . AspNetCore . Authorization ;
3
3
4
- public class ProfileController : Controller {
4
+ public class RequirePermissionAttribute : AuthorizeAttribute { }
5
+
6
+ public class ProfileController : Controller
7
+ {
5
8
private void doThings ( ) { }
6
9
private bool isAuthorized ( ) { return false ; }
7
10
8
11
// BAD: This is a Delete method, but no auth is specified.
9
- public ActionResult Delete1 ( int id ) {
12
+ public ActionResult Delete1 ( int id ) // $ Alert
13
+ {
10
14
doThings ( ) ;
11
15
return View ( ) ;
12
16
}
13
17
14
18
// GOOD: isAuthorized is checked.
15
- public ActionResult Delete2 ( int id ) {
16
- if ( ! isAuthorized ( ) ) {
19
+ public ActionResult Delete2 ( int id )
20
+ {
21
+ if ( ! isAuthorized ( ) )
22
+ {
17
23
return null ;
18
24
}
19
25
doThings ( ) ;
@@ -22,35 +28,49 @@ public ActionResult Delete2(int id) {
22
28
23
29
// GOOD: The Authorize attribute is used.
24
30
[ Authorize ]
25
- public ActionResult Delete3 ( int id ) {
31
+ public ActionResult Delete3 ( int id )
32
+ {
26
33
doThings ( ) ;
27
34
return View ( ) ;
28
35
}
29
36
37
+ // GOOD: The RequirePermission attribute is used (which extends AuthorizeAttribute).
38
+ [ RequirePermission ]
39
+ public ActionResult Delete4 ( int id )
40
+ {
41
+ doThings ( ) ;
42
+ return View ( ) ;
43
+ }
30
44
}
31
45
32
46
[ Authorize ]
33
- public class AuthBaseController : Controller {
47
+ public class AuthBaseController : Controller
48
+ {
34
49
protected void doThings ( ) { }
35
50
}
36
51
37
- public class SubController : AuthBaseController {
52
+ public class SubController : AuthBaseController
53
+ {
38
54
// GOOD: The Authorize attribute is used on the base class.
39
- public ActionResult Delete4 ( int id ) {
55
+ public ActionResult Delete4 ( int id )
56
+ {
40
57
doThings ( ) ;
41
58
return View ( ) ;
42
59
}
43
60
}
44
61
45
62
[ Authorize ]
46
- public class AuthBaseGenericController < T > : Controller {
63
+ public class AuthBaseGenericController < T > : Controller
64
+ {
47
65
protected void doThings ( ) { }
48
66
}
49
67
50
- public class SubGenericController : AuthBaseGenericController < string > {
68
+ public class SubGenericController : AuthBaseGenericController < string >
69
+ {
51
70
// GOOD: The Authorize attribute is used on the base class.
52
- public ActionResult Delete5 ( int id ) {
71
+ public ActionResult Delete5 ( int id )
72
+ {
53
73
doThings ( ) ;
54
74
return View ( ) ;
55
75
}
56
- }
76
+ }
0 commit comments