66use DevNet \System \Collections \ArrayList ;
77use DevNet \Web \Controller \AbstractController ;
88use DevNet \Web \Controller \IActionResult ;
9- use DevNet \Web \Security \Antiforgery \AntiForgeryFilter ;
10- use DevNet \Web \Security \Authorization \AuthorizeFilter ;
11- use DevNet \Web \Security \ClaimsPrincipal ;
12- use DevNet \Web \Security \ClaimsIdentity ;
13- use DevNet \Web \Security \ClaimType ;
14- use DevNet \Web \Security \Claim ;
9+ use DevNet \Web \Filters \AntiForgery ;
10+ use DevNet \Web \Filters \Authorize ;
11+ use DevNet \Web \Security \Claims \ClaimsIdentity ;
12+ use DevNet \Web \Security \Claims \ClaimType ;
13+ use DevNet \Web \Security \Claims \Claim ;
1514use Application \Models \Login ;
1615use Application \Models \Registration ;
1716use Application \Models \User ;
1817
1918/**
2019 * This is an example on how to create registration and login system using claims without SQL database.
21- * This example dosen't encrypt your data, so it's not recommanded for production,
20+ * This example dosen't encrypt the user password or data, so it's not recommanded for production,
2221 * Use DevNet Identity Manager instead, or encrypt you own data.
2322 */
23+ #[Authorize(roles: ['admin ' , 'member ' ])]
2424class AccountController extends AbstractController
2525{
26- public function __construct ()
27- {
28- $ this ->filter ('index ' , AuthorizeFilter::class);
29- $ this ->filter ('login ' , AntiForgeryFilter::class);
30- $ this ->filter ('register ' , AntiForgeryFilter::class);
31- }
32-
3326 public function index (): IActionResult
3427 {
3528 $ user = $ this ->HttpContext ->User ;
@@ -39,6 +32,8 @@ public function index(): IActionResult
3932 return $ this ->view ();
4033 }
4134
35+ #[Authorize]
36+ #[AntiForgery]
4237 public function login (Login $ form ): IActionResult
4338 {
4439 $ user = $ this ->HttpContext ->User ;
@@ -55,13 +50,13 @@ public function login(Login $form): IActionResult
5550 return $ this ->view ();
5651 }
5752
58- $ data = file_get_contents (__DIR__ . '/../data.json ' );
59- $ users = json_decode ($ data );
53+ $ json = file_get_contents (__DIR__ . '/../data.json ' );
54+ $ data = json_decode ($ json );
6055
61- $ userList = new ArrayList ('object ' );
62- $ userList ->addrange ($ users );
56+ $ users = new ArrayList ('object ' );
57+ $ users ->addrange ($ data );
6358
64- $ user = $ userList ->where (fn ($ user ) => $ user ->Username == $ form ->Username )->first ();
59+ $ user = $ users ->where (fn ($ user ) => $ user ->Username == $ form ->Username )->first ();
6560
6661 if (!$ user ) {
6762 return $ this ->view ();
@@ -74,35 +69,36 @@ public function login(Login $form): IActionResult
7469 $ identity = new ClaimsIdentity ('AuthenticationUser ' );
7570 $ identity ->addClaim (new Claim (ClaimType::Name, $ user ->Name ));
7671 $ identity ->addClaim (new Claim (ClaimType::Email, $ user ->Username ));
77- $ identity ->addClaim (new Claim (ClaimType::Role, 'Memeber ' ));
78- $ userPrincipal = new ClaimsPrincipal ($ identity );
72+ $ identity ->addClaim (new Claim (ClaimType::Role, 'member ' ));
7973 $ authentication = $ this ->HttpContext ->Authentication ;
80- $ authentication ->SignIn ( $ userPrincipal , $ form ->Remember );
74+ $ authentication ->signIn ( $ identity , $ form ->Remember );
8175
8276 return $ this ->redirect ('/account/index ' );
8377 }
8478
79+ #[Authorize]
80+ #[AntiForgery]
8581 public function register (Registration $ form ): IActionResult
8682 {
8783 $ this ->ViewData ['success ' ] = false ;
8884 if (!$ form ->isValide ()) {
8985 return $ this ->view ();
9086 }
9187
92- $ users = [];
88+ $ data = [];
9389 if (file_exists (__DIR__ . '/../data.json ' )) {
94- $ data = file_get_contents (__DIR__ . '/../data.json ' );
95- $ users = json_decode ($ data , true );
90+ $ json = file_get_contents (__DIR__ . '/../data.json ' );
91+ $ data = json_decode ($ json , true );
9692 }
9793
9894 $ user = new User ();
9995 $ user ->Name = $ form ->Name ;
10096 $ user ->Username = $ form ->Email ;
10197 $ user ->Password = $ form ->Password ;
10298
103- $ users [] = $ user ;
104- $ data = json_encode ($ users , JSON_PRETTY_PRINT );
105- file_put_contents (__DIR__ . '/../data.json ' , $ data );
99+ $ data [] = $ user ;
100+ $ json = json_encode ($ data , JSON_PRETTY_PRINT );
101+ file_put_contents (__DIR__ . '/../data.json ' , $ json );
106102
107103 $ this ->ViewData ['success ' ] = true ;
108104 return $ this ->view ();
0 commit comments