Skip to content

Commit 71c9619

Browse files
authored
Merge pull request #3 from byjg/2.0
3.0
2 parents 4eeed2f + 7d12763 commit 71c9619

15 files changed

+291
-239
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: php
22
php:
3+
- "7.2"
34
- "7.1"
45
- "7.0"
56
- "5.6"

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ you can use the UserDefinition and UserPropertiesDefinition classes for customiz
208208
<?php
209209
$userDefinition = new \ByJG\Authenticate\Definition\UserDefinition(
210210
'users', // $table
211+
\ByJG\Authenticate\Model\UserModel::class, // Model class
211212
\ByJG\Authenticate\Definition\UserDefinition::LOGIN_IS_EMAIL,
212213
[
213214
'userid' => 'fieldname of userid',

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
"prefer-stable": true,
1717
"require": {
1818
"php": ">=5.6.0",
19-
"byjg/micro-orm": "2.1.*",
20-
"byjg/cache-engine": "3.0.*",
19+
"byjg/micro-orm": "2.2.*",
20+
"byjg/cache-engine": "4.0.*",
2121
"byjg/jwt-wrapper": "1.0.*"
2222
},
23+
"require-dev": {
24+
"phpunit/phpunit": "5.7.*|6.5.*"
25+
},
2326
"license": "MIT"
2427
}

src/Definition/UserDefinition.php

+109-79
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@
44

55
use ByJG\Authenticate\Model\UserModel;
66
use ByJG\MicroOrm\Mapper;
7+
use ByJG\Serializer\BinderObject;
78

89
/**
910
* Structure to represent the users
1011
*/
1112
class UserDefinition
1213
{
13-
14-
protected $table = 'users';
15-
protected $userid = 'userid';
16-
protected $name = 'name';
17-
protected $email = 'email';
18-
protected $username = 'username';
19-
protected $password = 'password';
20-
protected $created = 'created';
21-
protected $admin = 'admin';
14+
protected $__table = 'users';
15+
protected $__closures = ["select" => [], "update" => [] ];
16+
protected $__loginField;
17+
protected $__model;
18+
protected $__properties = [];
2219

2320
const UPDATE="update";
2421
const SELECT="select";
2522

26-
protected $closures = [ "select" => [], "update" => [] ];
27-
28-
protected $loginField;
29-
30-
protected $model;
3123

3224
const LOGIN_IS_EMAIL="email";
3325
const LOGIN_IS_USERNAME="username";
@@ -36,109 +28,102 @@ class UserDefinition
3628
* Define the name of fields and table to store and retrieve info from database
3729
*
3830
* @param string $table
31+
* @param string $model
3932
* @param string $loginField
4033
* @param array $fieldDef
34+
* @throws \Exception
4135
*/
4236
public function __construct(
4337
$table = 'users',
38+
$model = UserModel::class,
4439
$loginField = self::LOGIN_IS_USERNAME,
4540
$fieldDef = []
4641
) {
47-
$this->table = $table;
42+
$this->__table = $table;
43+
$this->__model = $model;
44+
45+
// Set Default User Definition
46+
$modelInstance = $this->modelInstance();
47+
$modelProperties = BinderObject::toArrayFrom($modelInstance);
48+
foreach (array_keys($modelProperties) as $property) {
49+
$this->__properties[$property] = $property;
50+
}
4851

52+
// Set custom Properties
4953
foreach ($fieldDef as $property => $value) {
5054
$this->checkProperty($property);
51-
$this->{$property} = $value;
55+
$this->__properties[$property] = $value;
5256
}
5357

54-
$this->model = UserModel::class;
55-
5658
$this->defineClosureForUpdate('password', function ($value) {
57-
return strtoupper(sha1($value));
59+
// Already have a SHA1 password
60+
if (strlen($value) === 40) {
61+
return $value;
62+
}
63+
64+
// Leave null
65+
if (empty($value)) {
66+
return null;
67+
}
68+
69+
// Return the hash password
70+
return strtolower(sha1($value));
5871
});
5972

6073
if ($loginField !== self::LOGIN_IS_USERNAME && $loginField !== self::LOGIN_IS_EMAIL) {
6174
throw new \InvalidArgumentException('Login field is invalid. ');
6275
}
63-
$this->loginField = $loginField;
64-
}
76+
$this->__loginField = $loginField;
6577

66-
/**
67-
* @return string
68-
*/
69-
public function table()
70-
{
71-
return $this->table;
72-
}
73-
74-
/**
75-
* @return string
76-
*/
77-
public function getUserid()
78-
{
79-
return $this->userid;
78+
$this->beforeInsert = function ($instance) {
79+
return $instance;
80+
};
81+
$this->beforeUpdate = function ($instance) {
82+
return $instance;
83+
};
8084
}
8185

8286
/**
8387
* @return string
8488
*/
85-
public function getName()
89+
public function table()
8690
{
87-
return $this->name;
91+
return $this->__table;
8892
}
8993

90-
/**
91-
* @return string
92-
*/
93-
public function getEmail()
94-
{
95-
return $this->email;
96-
}
9794

98-
/**
99-
* @return string
100-
*/
101-
public function getUsername()
95+
public function __get($name)
10296
{
103-
return $this->username;
97+
$this->checkProperty($name);
98+
return $this->__properties[$name];
10499
}
105100

106-
/**
107-
* @return string
108-
*/
109-
public function getPassword()
101+
public function __call($name, $arguments)
110102
{
111-
return $this->password;
112-
}
113-
114-
/**
115-
* @return string
116-
*/
117-
public function getCreated()
118-
{
119-
return $this->created;
103+
if (strpos($name, 'get') === 0) {
104+
$name = strtolower(substr($name, 3));
105+
return $this->{$name};
106+
}
107+
throw new \InvalidArgumentException("Method '$name' does not exists'");
120108
}
121109

122-
/**
123-
* @return string
124-
*/
125-
public function getAdmin()
110+
public function toArray()
126111
{
127-
return $this->admin;
112+
return $this->__properties;
128113
}
129114

130115
/**
131116
* @return string
132117
*/
133118
public function loginField()
134119
{
135-
return $this->{"get" . $this->loginField}();
120+
return $this->{$this->__loginField};
136121
}
137122

138123
private function checkProperty($property)
139124
{
140-
if (!isset($this->{$property})) {
141-
throw new \InvalidArgumentException('Invalid property');
125+
if (!isset($this->__properties[$property])) {
126+
throw new \InvalidArgumentException("Property '$property' does not exists'");
142127
}
143128
}
144129

@@ -150,24 +135,33 @@ private function checkProperty($property)
150135
private function updateClosureDef($event, $property, $closure)
151136
{
152137
$this->checkProperty($property);
153-
$this->closures[$event][$property] = $closure;
138+
$this->__closures[$event][$property] = $closure;
154139
}
155140

156141
private function getClosureDef($event, $property)
157142
{
158143
$this->checkProperty($property);
159144

160-
// If does no exists event returns the default closure
161-
if (!isset($this->closures[$event])) {
145+
if (!$this->existsClosure($event, $property)) {
162146
return Mapper::defaultClosure();
163147
}
164148

165-
// If event exists but does no exists the property returns the default closure
166-
if (!array_key_exists($property, $this->closures[$event])) {
167-
return Mapper::defaultClosure();
149+
return $this->__closures[$event][$property];
150+
}
151+
152+
public function existsClosure($event, $property)
153+
{
154+
// Event not set
155+
if (!isset($this->__closures[$event])) {
156+
return false;
168157
}
169158

170-
return $this->closures[$event][$property];
159+
// Event is set but there is no property
160+
if (!array_key_exists($property, $this->__closures[$event])) {
161+
return false;
162+
}
163+
164+
return true;
171165
}
172166

173167
public function markPropertyAsReadOnly($property)
@@ -201,12 +195,48 @@ public function getClosureForSelect($property)
201195

202196
public function model()
203197
{
204-
return $this->model;
198+
return $this->__model;
205199
}
206200

207201
public function modelInstance()
208202
{
209-
$model = $this->model;
203+
$model = $this->__model;
210204
return new $model();
211205
}
206+
207+
protected $beforeInsert;
208+
209+
/**
210+
* @return mixed
211+
*/
212+
public function getBeforeInsert()
213+
{
214+
return $this->beforeInsert;
215+
}
216+
217+
/**
218+
* @param mixed $beforeInsert
219+
*/
220+
public function setBeforeInsert($beforeInsert)
221+
{
222+
$this->beforeInsert = $beforeInsert;
223+
}
224+
225+
protected $beforeUpdate;
226+
227+
/**
228+
* @return mixed
229+
*/
230+
public function getBeforeUpdate()
231+
{
232+
return $this->beforeUpdate;
233+
}
234+
235+
/**
236+
* @param mixed $beforeUpdate
237+
*/
238+
public function setBeforeUpdate($beforeUpdate)
239+
{
240+
$this->beforeUpdate = $beforeUpdate;
241+
}
212242
}

src/Interfaces/UsersInterface.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function addUser($name, $userName, $email, $password);
3636
* @param $model
3737
* @return bool
3838
*/
39-
public function add($model);
39+
public function canAddUser($model);
4040

4141
/**
4242
* @desc Get the user based on a filter
@@ -60,6 +60,13 @@ public function getById($userid);
6060
*/
6161
public function getByEmail($email);
6262

63+
/**
64+
* @desc Get the user based on his username.
65+
* @param $username
66+
* @return Row if user was found; null, otherwise
67+
*/
68+
public function getByUsername($username);
69+
6370
/**
6471
* @desc Get the user based on his login
6572
* @param string $login
@@ -174,8 +181,8 @@ public function getUserDefinition();
174181
public function getUserPropertiesDefinition();
175182

176183
/**
177-
* Return the ID for the user id (if it is not autoincrement)
178-
* @return mixed
184+
* @param $userid
185+
* @return void
179186
*/
180-
public function generateUserId();
187+
public function removeUserById($userid);
181188
}

0 commit comments

Comments
 (0)