1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* UserController Managers user activity login, register and logout |
5
|
|
|
* |
6
|
|
|
* @package Ibonly\NaijaEmoji\UserController |
7
|
|
|
* @author Ibraheem ADENIYI <[email protected]> |
8
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ibonly\NaijaEmoji; |
12
|
|
|
|
13
|
|
|
use Slim\Slim; |
14
|
|
|
use Ibonly\NaijaEmoji\User; |
15
|
|
|
use Ibonly\NaijaEmoji\UserInterface; |
16
|
|
|
use Ibonly\NaijaEmoji\AuthController; |
17
|
|
|
use Ibonly\PotatoORM\DataNotFoundException; |
18
|
|
|
use Ibonly\NaijaEmoji\InvalidTokenException; |
19
|
|
|
use Ibonly\NaijaEmoji\ProvideTokenException; |
20
|
|
|
use Ibonly\NaijaEmoji\PasswordExistException; |
21
|
|
|
use Ibonly\PotatoORM\DataAlreadyExistException; |
22
|
|
|
|
23
|
|
|
class UserController implements UserInterface |
24
|
|
|
{ |
25
|
|
|
protected $user; |
26
|
|
|
protected $auth; |
27
|
|
|
|
28
|
|
|
public function __construct () |
29
|
|
|
{ |
30
|
|
|
$this->user = new User(); |
31
|
|
|
$this->auth = new AuthController(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* createUser Create a new user |
36
|
|
|
* |
37
|
|
|
* @param $app |
38
|
|
|
* |
39
|
|
|
* @return json |
40
|
|
|
*/ |
41
|
|
|
public function createUser (Slim $app) |
42
|
|
|
{ |
43
|
|
|
$username = $app->request->params('username'); |
44
|
|
|
$this->user->id = NULL; |
|
|
|
|
45
|
|
|
$this->user->username = $username; |
|
|
|
|
46
|
|
|
$this->user->password = $this->auth->passwordEncrypt($app->request->params('password')); |
|
|
|
|
47
|
|
|
$this->user->date_created = date('Y-m-d H:i:s'); |
|
|
|
|
48
|
|
|
try |
49
|
|
|
{ |
50
|
|
|
$save = $this->user->save(); |
51
|
|
|
if( $save ) |
52
|
|
|
$app->halt(201, json_encode(['message' => 'Registration Successful. Please Login to generate your token'])); |
53
|
|
|
} catch ( DataAlreadyExistException $e ) { |
54
|
|
|
$app->halt(404, json_encode(['message' => 'User details already exist'])); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* login Log user in and generate token |
60
|
|
|
* |
61
|
|
|
* @param $app |
62
|
|
|
* |
63
|
|
|
* @return json |
64
|
|
|
*/ |
65
|
|
|
public function login (Slim $app) |
66
|
|
|
{ |
67
|
|
|
$app->response->headers->set('Content-Type', 'application/json'); |
68
|
|
|
$username = $app->request->params('username'); |
69
|
|
|
$password = $app->request->params('password'); |
70
|
|
|
try |
71
|
|
|
{ |
72
|
|
|
//check if username is available |
73
|
|
|
$login = $this->user->where(['username' => $username])->toJson(); |
74
|
|
|
if( ! empty ($login) ) |
75
|
|
|
$hashPassword = ""; |
76
|
|
|
$output = json_decode($login); |
77
|
|
|
foreach( $output as $key ) |
78
|
|
|
{ |
79
|
|
|
$output = $key->id; |
80
|
|
|
$hashPassword = $key->password; |
81
|
|
|
} |
82
|
|
|
//confirm the password |
83
|
|
|
return $this->decryptPassword($username, $password, $hashPassword); |
|
|
|
|
84
|
|
|
} catch ( DataNotFoundException $e ) { |
85
|
|
|
$app->halt(404, json_encode(['message' => 'Not Found'])); |
86
|
|
|
} catch ( PasswordException $e ) { |
87
|
|
|
return $e->errorMessage(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* decryptPassword and return token |
93
|
|
|
* |
94
|
|
|
* @param $user |
95
|
|
|
* @param $password |
96
|
|
|
* @param $hashPassword |
97
|
|
|
*/ |
98
|
|
|
public function decryptPassword ($username, $password, $hashPassword) |
99
|
|
|
{ |
100
|
|
|
if( $this->auth->passwordDecrypt($password, $hashPassword) ) |
101
|
|
|
return(json_encode([ |
102
|
|
|
'Username' => $username, |
103
|
|
|
'Authorization' => $this->auth->authorizationEncode($username) |
104
|
|
|
])); |
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* logout Log user out and destroy token |
108
|
|
|
* |
109
|
|
|
* @param $app |
110
|
|
|
* |
111
|
|
|
* @return json |
112
|
|
|
*/ |
113
|
|
|
public function logout (Slim $app) |
114
|
|
|
{ |
115
|
|
|
$app->response->headers->set('Content-Type', 'application/json'); |
116
|
|
|
$tokenData = $app->request->headers->get('Authorization'); |
117
|
|
|
try |
118
|
|
|
{ |
119
|
|
|
if ( ! isset( $tokenData ) ) |
120
|
|
|
throw new ProvideTokenException(); |
121
|
|
|
|
122
|
|
|
$checkUser = $this->user->where(['username' => $tokenData->user])->toJson(); |
123
|
|
|
if ( ! empty ($checkUser) ) |
124
|
|
|
$this->auth->authorizationEncode(NULL);# |
125
|
|
|
$app->halt(200, json_encode(['message' => 'Logged out Successfully'])); |
126
|
|
|
} catch ( DataNotFoundException $e) { |
127
|
|
|
$app->halt(404, json_encode(['message' => 'Not Found'])); |
128
|
|
|
} catch ( InvalidTokenException $e ) { |
129
|
|
|
$app->halt(405, json_encode(['Message' => 'Invalid Token'])); |
130
|
|
|
} catch ( ProvideTokenException $e ) { |
131
|
|
|
$app->halt(406, json_encode(['Message' => 'Enter a valid Token'])); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.