Skip to content

Commit 758c319

Browse files
authored
Merge pull request #32 from kasunmendis7/feature/admin-login
added admin login
2 parents 4e1c8d8 + a3da490 commit 758c319

File tree

6 files changed

+371
-1
lines changed

6 files changed

+371
-1
lines changed

controllers/AuthController.php

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use app\core\Request;
88
use app\core\Response;
99
use app\models\Admin;
10+
use app\models\AdminLogin;
1011
use app\models\Customer;
1112
use app\models\CustomerLoginForm;
1213

@@ -186,4 +187,28 @@ public function adminSignUp(Request $request)
186187
]);
187188
}
188189

190+
/* admin login method */
191+
public function adminLogin(Request $request, Response $response)
192+
{
193+
$adminLogin = new AdminLogin();
194+
if ($request->isPost()) {
195+
$adminLogin->loadData($request->getBody());
196+
if ($adminLogin->validate() && $adminLogin->login()) {
197+
$response->redirect('/admin-dashboard'); // later will change this to admin dashboard
198+
return;
199+
}
200+
}
201+
$this->setLayout('auth');
202+
return $this->render('/admin/admin-login', [
203+
'model' => $adminLogin
204+
]);
205+
}
206+
207+
/* admin logout method */
208+
public function adminLogout(Request $request, Response $response)
209+
{
210+
Application::$app->logoutAdmin();
211+
$response->redirect('/');
212+
}
213+
189214
}

controllers/AuthController.php~

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
use app\core\Application;
6+
use app\core\Controller;
7+
use app\core\Request;
8+
use app\core\Response;
9+
use app\models\Admin;
10+
use app\models\Customer;
11+
use app\models\CustomerLoginForm;
12+
13+
//use app\core\Response;
14+
use app\models\CustomerRegisterModel;
15+
use app\models\ServiceCenter;
16+
use app\models\ServiceCenterLogin;
17+
use app\models\Technician;
18+
use app\models\ServiceCentre;
19+
use app\models\TechnicianLogin;
20+
21+
class AuthController extends Controller
22+
{
23+
/* customer sign up method */
24+
public function customerSignUp(Request $request)
25+
{
26+
$customer = new Customer();
27+
if ($request->isPost()) {
28+
29+
$customer->loadData($request->getBody());
30+
if ($customer->validate() && $customer->save()) {
31+
Application::$app->session->setFlash('success', 'You have been registered successfully!');
32+
Application::$app->response->redirect('/customer-login');
33+
}
34+
$this->setLayout('auth');
35+
return $this->render('/customer/customer-sign-up', [
36+
'model' => $customer
37+
]);
38+
}
39+
$this->setLayout('auth');
40+
return $this->render('/customer/customer-sign-up', [
41+
'model' => $customer
42+
]);
43+
}
44+
45+
/* customer login method */
46+
public function customerLogin(Request $request, Response $response)
47+
{
48+
$loginForm = new CustomerLoginForm();
49+
if ($request->isPost()) {
50+
$loginForm->loadData($request->getBody());
51+
if ($loginForm->validate() && $loginForm->login()) {
52+
$response->redirect('/customer-dashboard'); // later will change this to customer dashboard
53+
$customer = new Customer();
54+
$customer->customerAddressGeocoding();
55+
return;
56+
}
57+
}
58+
$this->setLayout('auth');
59+
return $this->render('/customer/customer-login', [
60+
'model' => $loginForm
61+
]);
62+
}
63+
64+
/* customer logout method */
65+
public function customerLogout(Request $request, Response $response)
66+
{
67+
Application::$app->logoutCustomer();
68+
$response->redirect('/');
69+
}
70+
71+
/* technician sign up method */
72+
public function technicianSignUp(Request $request)
73+
{
74+
$technician = new Technician();
75+
if ($request->isPost()) {
76+
$technician->loadData($request->getBody());
77+
78+
if ($technician->validate() && $technician->save()) {
79+
Application::$app->session->setFlash('success', 'You have been registered successfully!');
80+
Application::$app->response->redirect('/technician-login');
81+
}
82+
$this->setLayout('auth');
83+
return $this->render('/technician/technician-sign-up', [
84+
'model' => $technician
85+
]);
86+
}
87+
$this->setLayout('auth');
88+
return $this->render('/technician/technician-sign-up', [
89+
'model' => $technician
90+
]);
91+
}
92+
93+
// technician login method
94+
public function technicianLogin(Request $request, Response $response)
95+
{
96+
$technicianLogin = new TechnicianLogin();
97+
if ($request->isPost()) {
98+
$technicianLogin->loadData($request->getBody());
99+
if ($technicianLogin->validate() && $technicianLogin->loginTechnician()) {
100+
$response->redirect('/technician-dashboard');
101+
$technician = new Technician();
102+
$technician->technicianAddressGeocoding();
103+
return;
104+
}
105+
}
106+
$this->setLayout('auth');
107+
return $this->render('/technician/technician-login', ['model' => $technicianLogin]);
108+
}
109+
110+
public function technicianLogOut(Request $request, Response $response)
111+
{
112+
Application::$app->logoutTechnician();
113+
$response->redirect('/');
114+
}
115+
116+
117+
/* service centre sign up method */
118+
119+
public function serviceCentreSignup(Request $request)
120+
{
121+
$registerModel = new ServiceCenter();
122+
if ($request->isPost()) {
123+
$registerModel->loadData($request->getBody());
124+
125+
if ($registerModel->validate() && $registerModel->save()) {
126+
Application::$app->session->setFlash('success', 'You have been registered successfully!');
127+
Application::$app->response->redirect('/service-centre-login');
128+
}
129+
$this->setLayout('auth');
130+
return $this->render('/service-centre/service-centre-sign-up', [
131+
'model' => $registerModel
132+
]);
133+
}
134+
$this->setLayout('auth');
135+
return $this->render('/service-centre/service-centre-sign-up', [
136+
'model' => $registerModel
137+
]);
138+
}
139+
140+
/* service centre login method */
141+
// public function serviceCentreLogin(Request $request)
142+
// service centre login method
143+
public function serviceCentreLogin(Request $request, Response $response)
144+
{
145+
$serviceCenterLogin = new ServiceCenterLogin();
146+
if ($request->isPost()) {
147+
$serviceCenterLogin->loadData($request->getBody());
148+
if ($serviceCenterLogin->validate() && $serviceCenterLogin->loginServiceCenter()) {
149+
$response->redirect('/service-centre-dashboard');
150+
$service_centre = new ServiceCenter();
151+
$service_centre->serviceCentreAddressGeocoding();
152+
return;
153+
}
154+
}
155+
$this->setLayout('auth');
156+
return $this->render('/service-centre/service-centre-login', [
157+
'model' => $serviceCenterLogin
158+
]);
159+
}
160+
161+
public function serviceCenterLogout(Request $request, Response $response)
162+
{
163+
Application::$app->logoutServiceCenter();
164+
$response->redirect('/service-centre-landing');
165+
}
166+
167+
/* admin sign up method */
168+
public function adminSignUp(Request $request)
169+
{
170+
$admin = new Admin();
171+
if ($request->isPost()) {
172+
173+
$admin->loadData($request->getBody());
174+
if ($admin->validate() && $admin->save()) {
175+
Application::$app->session->setFlash('success', 'You have been registered successfully!');
176+
Application::$app->response->redirect('/admin-login');
177+
}
178+
$this->setLayout('auth');
179+
return $this->render('/admin/admin-sign-up', [
180+
'model' => $admin
181+
]);
182+
}
183+
$this->setLayout('auth');
184+
return $this->render('/admin/admin-sign-up', [
185+
'model' => $admin
186+
]);
187+
}
188+
189+
}

core/Application.php

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Application
1111
public string $serviceCenterClass;
1212
public static Application $app;
1313
public string $customerClass;
14+
public string $adminClass;
1415
public Router $router;
1516
public Request $request;
1617
public Response $response;
@@ -27,6 +28,7 @@ public function __construct($rootPath, array $config)
2728
$this->customerClass = $config['customerClass'];
2829
$this->technicianClass = $config['technicianClass'];
2930
$this->serviceCenterClass = $config['serviceCenterClass'];
31+
$this->adminClass = $config['adminClass'];
3032
self::$ROOT_DIR = $rootPath;
3133
self::$app = $this;
3234
$this->request = new Request();
@@ -65,6 +67,15 @@ public function __construct($rootPath, array $config)
6567
} else {
6668
$this->serviceCenter = null;
6769
}
70+
71+
$primaryValueAdmin = $this->session->get('admin');
72+
if ($primaryValueAdmin) {
73+
$adminInstance = new $this->adminClass;
74+
$primaryKey = $adminInstance->primaryKey();
75+
$this->admin = $adminInstance->findOne([$primaryKey => $primaryValueAdmin]);
76+
} else {
77+
$this->admin = null;
78+
}
6879
}
6980

7081
public function loginCustomer(DbModel $customer)
@@ -133,4 +144,19 @@ public function logoutServiceCenter()
133144
$this->session->remove('serviceCenter');
134145
}
135146

147+
public function loginAdmin(DbModel $admin)
148+
{
149+
$this->admin = $admin;
150+
$primaryKey = $admin->primaryKey();
151+
$primaryValue = $admin->{$primaryKey};
152+
$this->session->set('admin', $primaryValue);
153+
return true;
154+
}
155+
156+
public function logoutAdmin()
157+
{
158+
$this->admin = null;
159+
$this->session->remove('admin');
160+
}
161+
136162
}

models/AdminLogin.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
use app\core\Application;
6+
use app\core\Model;
7+
use app\models\Admin;
8+
9+
class AdminLogin extends Model
10+
{
11+
12+
public string $email = '';
13+
public string $password = '';
14+
15+
public function rules(): array
16+
{
17+
return [
18+
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
19+
'password' => [self::RULE_REQUIRED]
20+
];
21+
}
22+
23+
public function updateRules(): array
24+
{
25+
return [];
26+
}
27+
28+
public function login()
29+
{
30+
$adminModel = new Admin();
31+
$admin = $adminModel->findOne(['email' => $this->email]);
32+
if (!$admin) {
33+
$this->addErrorMessage('email', 'User does not exist with this email');
34+
return false;
35+
}
36+
37+
if (!password_verify($this->password, $admin->password)) {
38+
$this->addErrorMessage('password', 'Password is incorrect');
39+
return false;
40+
}
41+
42+
show($admin);
43+
44+
return Application::$app->loginAdmin($admin);
45+
}
46+
}

public/index.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'technicianClass' => \app\models\Technician::class,
2323
'customerClass' => \app\models\Customer::class,
2424
'serviceCenterClass' => \app\models\ServiceCenter::class,
25+
'adminClass' => \app\models\Admin::class,
2526
'db' => [
2627
'dsn' => $_ENV['DB_DSN'],
2728
'user' => $_ENV['DB_USER'],
@@ -98,8 +99,11 @@
9899
/* Admin Auth routes */
99100
$app->router->get('/admin-sign-up', [AuthController::class, 'adminSignUp']);
100101
$app->router->post('/admin-sign-up', [AuthController::class, 'adminSignUp']);
102+
$app->router->get('/admin-login', [AuthController::class, 'adminLogin']);
103+
$app->router->post('/admin-login', [AuthController::class, 'adminLogin']);
104+
$app->router->get('/admin-logout', [AuthController::class, 'adminLogout']);
101105

102-
/* routes related to the by Post */
106+
/* Routes related to the by Post */
103107
$app->router->get('/technician-create-post', [TechnicianController::class, 'technicianCreatePost']);
104108
$app->router->get('/technician-edit-post', [TechnicianController::class, 'technicianEditPost']);
105109
$app->router->post('/technician-create-post', [PostController::class, 'create']);

0 commit comments

Comments
 (0)