1616class Auth
1717{
1818 /**
19- * @var string token storage table name
19+ * Tokens storage table.
20+ *
21+ * @var string
2022 */
2123 public static $ api_table = 'api_auth ' ;
2224
@@ -40,11 +42,24 @@ class Auth
4042 * @var Request
4143 */
4244 public static $ settings ;
43-
44- public $ user = [];
45+ /**
46+ * User data.
47+ *
48+ * @var object
49+ */
50+ public $ user ;
51+ /**
52+ * Account associated to the user.
53+ *
54+ * @var object
55+ */
56+ public $ account ;
4557 public $ user_id ;
4658 public $ is_admin = false ;
4759 public $ authenticated = false ;
60+ /**
61+ * @var PDO
62+ */
4863 private $ db ;
4964 private $ table_free = [];
5065 private $ table_readonly = [];
@@ -63,7 +78,7 @@ public function __construct()
6378 /**
6479 * Get user row.
6580 *
66- * @return array
81+ * @return object
6782 */
6883 public static function getUser ()
6984 {
@@ -113,7 +128,7 @@ public function validate($query)
113128 $ this ->checkAPITable ();
114129 }
115130
116- if (!empty ($ this ->query ['check_counter ' ]) && $ this ->validateToken ($ this ->query ['token ' ]) && $ this ->is_admin ) {
131+ if (!empty ($ this ->query ['check_counter ' ]) && $ this ->validateToken ($ this ->query ['token ' ]) && $ this ->isAdmin () ) {
117132 $ this ->checkCounter ();
118133 } elseif (!empty ($ this ->query ['check_token ' ]) && $ this ->validateToken ($ this ->query ['check_token ' ])) {
119134 $ this ->checkToken ();
@@ -165,7 +180,7 @@ public function validate($query)
165180 if ($ user_row [$ users_columns ['password ' ]] == $ password ) {
166181 $ token = $ this ->generateToken ($ user_row [$ users_columns ['id ' ]], $ user_row [$ users_columns ['username ' ]]);
167182 $ this ->user_id = $ user_row [$ users_columns ['id ' ]];
168- $ this ->is_admin = !empty ($ users_columns ['admin ' ]) ? $ user_row [key (reset ($ users_columns ['admin ' ]))] : false ;
183+ $ this ->setIsAdmin ( !empty ($ users_columns ['admin ' ]) ? $ user_row [key (reset ($ users_columns ['admin ' ]))] : false ) ;
169184 // Render
170185 $ results = [
171186 (object )[
@@ -301,7 +316,7 @@ private function validateToken($token)
301316 $ this ->user = $ user_row ;
302317 $ this ->user_id = $ user_row ['id ' ];
303318 if (!empty ($ users_columns ['admin ' ])) {
304- $ this ->is_admin = (($ user_row [key ($ users_columns ['admin ' ])] == reset ($ users_columns ['admin ' ])) ? true : false );
319+ $ this ->setIsAdmin (($ user_row [key ($ users_columns ['admin ' ])] == reset ($ users_columns ['admin ' ])) ? true : false );
305320 }
306321 $ this ->authenticated = true ;
307322
@@ -367,7 +382,7 @@ public function generateToken($user_id, $user_name)
367382 {
368383 $ this ->db = $ this ->getAPIDatabase ();
369384 try {
370- $ token = md5 (uniqid (rand (), true ));
385+ $ token = md5 (uniqid (mt_rand (), true ));
371386 $ sth = $ this ->db ->prepare ('INSERT INTO ' . self ::$ api_table . ' (token,user_id,user_name,user_agent) VALUES (:token,:user_id,:user_name,:user_agent) ' );
372387 $ sth ->bindParam (':token ' , $ token );
373388 $ sth ->bindParam (':user_name ' , $ user_name );
@@ -398,7 +413,7 @@ public function permissionSQL($table, $permission)
398413 $ sql = '' ;
399414
400415 // All allowed
401- if ($ this ->is_admin == true ) {
416+ if ($ this ->isAdmin () ) {
402417 $ sql = "'1' = '1' " ;
403418 }
404419
@@ -524,7 +539,43 @@ public function canDelete($table)
524539 */
525540 private function needIncrementCounter ()
526541 {
527- return !(!empty ($ this ->query ['docs ' ]) || !empty ($ this ->query ['check_token ' ]) || !empty ($ this ->query ['check_counter ' ]) || !empty ($ this ->query ['user_id ' ]) && !empty ($ this ->query ['password ' ]));
542+ return !(
543+ !empty ($ this ->query ['docs ' ]) ||
544+ !empty ($ this ->query ['check_token ' ]) ||
545+ !empty ($ this ->query ['check_counter ' ]) ||
546+ (
547+ !empty ($ this ->query ['user_id ' ]) &&
548+ !empty ($ this ->query ['password ' ])
549+ )
550+ );
551+ }
552+
553+ /**
554+ * @return bool
555+ */
556+ public function isAuthenticated ()
557+ {
558+ return !empty (self ::getUser ());
559+ }
560+
561+ /**
562+ * @return bool
563+ */
564+ public function isAdmin ()
565+ {
566+ return $ this ->is_admin === true ;
567+ }
568+
569+ /**
570+ * @param bool $is_admin
571+ *
572+ * @return self
573+ */
574+ public function setIsAdmin ($ is_admin )
575+ {
576+ $ this ->is_admin = $ is_admin ;
577+
578+ return $ this ;
528579 }
529580}
530581
0 commit comments