33namespace Franky5831 \CodeIgniter4UserLibrary \Controllers ;
44
55use CodeIgniter \Controller ;
6+ use Exception ;
67use Franky5831 \CodeIgniter4UserLibrary \Models \User as UserModel ;
78
89class User extends Controller
910{
11+ private $ userModel ;
12+
1013 public function __construct ()
1114 {
1215 // Loads the user helper
1316 helper ('user_helper ' );
1417 // Adds form validation user rules
1518 config ('Validation ' )->ruleSets [] = \Franky5831 \CodeIgniter4UserLibrary \Validation \ValidationRules::class;
19+
20+ $ this ->userModel = new UserModel ();
1621 }
1722
1823 public function login (): \CodeIgniter \HTTP \RedirectResponse |string
1924 {
25+ $ config = config (\Franky5831 \CodeIgniter4UserLibrary \Config \App::class);
26+ $ userCanLogin = $ config ->userCanLogin ;
27+ if (!$ userCanLogin ) {
28+ throw \CodeIgniter \Exceptions \PageNotFoundException::forPageNotFound ();
29+ }
30+
2031 if (isLoggedIn ()) {
2132 throw \CodeIgniter \Exceptions \PageNotFoundException::forPageNotFound ();
2233 }
34+
2335 $ validationRules = [
2436 'email ' => [
2537 'label ' => 'Email ' ,
@@ -34,13 +46,16 @@ public function login(): \CodeIgniter\HTTP\RedirectResponse|string
3446 $ captchaRules = $ this ->getCaptchaRules ();
3547 $ validationRules = array_merge ($ validationRules , $ captchaRules );
3648
37- if ($ this ->request ->getMethod () == "POST " && $ this -> validate ( $ validationRules ) ) {
38- $ userModel = new userModel ();
39- $ user = $ userModel ->where ("email " , $ this ->request ->getPost ("email " ))->first ();
49+ if ($ this ->request ->getMethod () == "POST " ) {
50+ if ( $ this -> userModel -> getUserCanPost () && $ this -> validate ( $ validationRules )) {
51+ $ user = $ this -> userModel ->where ("email " , $ this ->request ->getPost ("email " ))->first ();
4052
41- $ this ->setUserMethod ($ user );
53+ $ this ->setUserMethod ($ user );
4254
43- return redirect ()->to ("/ " );
55+ return redirect ()->to ("/ " );
56+ } else {
57+ $ this ->userModel ->setPostError ();
58+ }
4459 }
4560 try {
4661 // Returns the view from the app's folder, if it doesn't exist, it returns the vendor's view
@@ -81,27 +96,29 @@ public function register(): \CodeIgniter\HTTP\RedirectResponse|string
8196 $ userExtraAttributes = $ config ->userExtraAttributes ;
8297 $ validationRules = array_merge ($ validationRules , $ captchaRules , $ userExtraAttributes );
8398
84- if ($ this ->request ->getMethod () == "POST " && $ this ->validate ($ validationRules )) {
85- $ userModel = new UserModel ();
86-
87- $ userData = [
88- 'email ' => $ this ->request ->getPost ('email ' ),
89- 'password ' => $ this ->request ->getPost ('password ' ),
90- ];
91- foreach ($ userExtraAttributes as $ attribute => $ data ) {
92- $ attributeValue = $ this ->request ->getPost ($ attribute );
93- $ userData [$ attribute ] = $ attributeValue ;
94- }
99+ if ($ this ->request ->getMethod () == "POST " ) {
100+ if ($ this ->userModel ->getUserCanPost () && $ this ->validate ($ validationRules )) {
101+ $ userData = [
102+ 'email ' => $ this ->request ->getPost ('email ' ),
103+ 'password ' => $ this ->request ->getPost ('password ' ),
104+ ];
105+ foreach ($ userExtraAttributes as $ attribute => $ data ) {
106+ $ attributeValue = $ this ->request ->getPost ($ attribute );
107+ $ userData [$ attribute ] = $ attributeValue ;
108+ }
95109
96- $ userModel ->save ($ userData );
97- $ session = session ();
110+ $ this -> userModel ->save ($ userData );
111+ $ session = session ();
98112
99- $ user = $ userModel ->where ("email " , $ this ->request ->getPost ("email " ))->first ();
100- $ this ->setUserMethod ($ user );
113+ $ user = $ this -> userModel ->where ("email " , $ this ->request ->getPost ("email " ))->first ();
114+ $ this ->setUserMethod ($ user );
101115
102- $ session ->setFlashdata ('success ' , "Registrazione avvenuta con successo " );
116+ $ session ->setFlashdata ('success ' , "Registrazione avvenuta con successo " );
103117
104- return redirect ()->to ('/ ' );
118+ return redirect ()->to ('/ ' );
119+ } else {
120+ $ this ->userModel ->setPostError ();
121+ }
105122 }
106123
107124 try {
@@ -135,9 +152,8 @@ private function getCaptchaRules(): array
135152 throw new \Exception ("The selected captcha type does not exists " , 1 );
136153 break ;
137154 }
138-
139- return $ validationRules ;
140155 }
156+ return $ validationRules ;
141157 }
142158
143159 private function setUserMethod ($ user ): void
0 commit comments