summaryrefslogtreecommitdiff
path: root/public/app/models/admin/User_model.php
diff options
context:
space:
mode:
Diffstat (limited to 'public/app/models/admin/User_model.php')
-rw-r--r--public/app/models/admin/User_model.php263
1 files changed, 263 insertions, 0 deletions
diff --git a/public/app/models/admin/User_model.php b/public/app/models/admin/User_model.php
new file mode 100644
index 0000000..62d99db
--- /dev/null
+++ b/public/app/models/admin/User_model.php
@@ -0,0 +1,263 @@
+<?php
+
+namespace app\models\admin;
+
+use \Registry;
+use app\models\admin\History_model as History;
+
+class User_model
+{
+
+ /** check user (by index key)
+ *
+ * @param $indexKey (string) : key for user identification
+ * @param $value (string)
+ *
+ * @param $email (string): user's email
+ */
+ public static function checkUser($email)
+ {
+ $user = Registry::use('database')->query(
+ "SELECT * FROM user WHERE email = :email AND active = 1",
+ [ ':email' => $email ]
+ )->getFirst();
+
+ // if no user, return false
+ if ($user === false) return false;
+
+ return $user;
+ }
+
+
+ /** get user (by index key)
+ *
+ * user detailed array
+ * includes all user properties + granted roles + privileges
+ *
+ * @param $id (int) : user id
+ * @param $value (string)
+ */
+ public static function getUser($id)
+ {
+ $user = Registry::use('database')->query(
+ "SELECT user.*,
+ ( -- construct array (json) of roles granted to user
+ SELECT CONCAT(
+ '[',
+ GROUP_CONCAT(role.id),
+ ']'
+ )
+ FROM `role`
+ WHERE role.id IN (
+ SELECT user_role.role_id
+ FROM user_role
+ WHERE user_role.user_id = :id
+ )
+ ) AS Roles_json,
+ ( -- construct array of (root-)privileges granted to user
+ SELECT CONCAT(
+ '[',
+ GROUP_CONCAT(privilege.id),
+ ']'
+ )
+ FROM privilege
+ WHERE privilege.id IN (
+ SELECT user_privilege.privilege_id
+ FROM user_privilege
+ WHERE user_privilege.user_id = :id
+ )
+ ) AS RootPrivileges_json,
+ ( -- construct array of (root-)privileges granted to user
+ SELECT CONCAT(
+ '[',
+ GROUP_CONCAT(privilege.includes),
+ ']'
+ )
+ FROM privilege
+ WHERE privilege.id IN (
+ SELECT user_privilege.privilege_id
+ FROM user_privilege
+ WHERE user_privilege.user_id = :id
+ )
+ ) AS SubPrivileges_json
+ FROM user
+ WHERE id = :id",
+ [ ':id' => $id ]
+ )->getFirst();
+
+
+ // if no user, return false
+ if ($user === false) return false;
+
+
+ // TODO:
+ // * merge root+sub privilede lists
+ // * convert json strings to php arrays
+
+
+ // TODO:
+ // cache user super array
+
+ return $user;
+ }
+
+
+ /** create user
+ *
+ * creates user record;
+ * assigns privileged (usualy defaults);
+ * creates activation_code
+ *
+ * @param $data (array): Request->POST array
+ * @param $password (string): secure hashed password
+ *
+ * @return $activation_code
+ *
+ */
+ public static function registerUser($data, $password, $privileges = DEFAULT_PRIVILEGES)
+ {
+ $required_fields = [
+ 'name',
+ 'surname',
+ 'email',
+ 'password'
+ ];
+
+ // check required fields
+ $isOK = true;
+ foreach($required_fields as $fi) {
+ if (empty($data[$fi])) $isOK = false;
+ }
+ // if empty required fields exists ... return false
+ if (!$isOK) {
+ return [ "success" => false, 'error' => EMPTY_REQUIRED_FIELDS ];
+ }
+
+
+ // TODO:
+ // check if email exists
+ // ...
+
+ // create an activation code
+ $activation_code = md5($data['email'].time().rand(0, 10000));
+
+ // if isOK go on and...
+ // create user record
+ $new_user_id = Registry::use('database')->query(
+ "INSERT INTO user
+ (`first_name`, `last_name`, `email`, `password`, `active`, `activation`)
+ VALUES
+ (:nam, :surname, :email, :pass, :act, :actcode)",
+ [
+ ':nam' => $data['name'],
+ ':surname' => $data['surname'],
+ ':email' => $data['email'],
+ ':pass' => $password,
+ ':act' => 0, // needs email confirmation to be activated ...
+ ':actcode' => $activation_code // ... with the activation code
+ ]
+ )->lastInsertID();
+
+ // set default privileges
+ // self::set_user_privileges($new_user_id, $privileges);
+
+ // update history
+ History::trackUserAccess($new_user_id, TRACK_ACCOUNT, 'Create User Account');
+
+ // return success and user id
+ return [
+ "success" => true,
+ 'id' => $new_user_id ,
+ 'activation' => $activation_code
+ ];
+ }
+
+
+ /** activate
+ *
+ * check if activation code is valid;
+ * if valid, set account active;
+ *
+ * @param $ticket (hex/MD5): activation code;
+ *
+ */
+ public static function activate($ticket)
+ {
+ $user = Registry::use('database')->query(
+ "SELECT * FROM user WHERE activation = :ticket",
+ [ 'ticket' => $ticket ]
+ )->getFirst();
+
+ // if no user with this activation code, return false
+ if ($user === false) return false;
+
+ // remove activation code from user record
+ Registry::use('database')->runQuery(
+ "UPDATE user
+ SET active = 1, `activation` = NULL
+ WHERE activation = :ticket",
+ [ 'ticket' => $ticket ]
+ );
+
+ // update history
+ History::trackUserAccess($user['id'], TRACK_ACCOUNT, 'User Account Activated');
+
+ return true;
+
+ }
+
+
+}
+
+/* example query getUser (super-array)
+--- -- -- - - -
+
+SELECT user.*,
+( -- array (json) of roles granted to user
+ SELECT CONCAT('[', GROUP_CONCAT(role.id), ']')
+ FROM `role`
+ WHERE role.id IN (
+ SELECT user_role.role_id
+ FROM user_role
+ WHERE user_role.user_id = 1
+ )
+) AS Roles_json,
+(
+ SELECT CONCAT(
+ '[',
+ GROUP_CONCAT(privilege.id),
+ ']'
+ )
+ FROM privilege
+ WHERE privilege.id IN (
+ SELECT user_privilege.privilege_id
+ FROM user_privilege
+ WHERE user_privilege.user_id = 1
+ )
+) AS RootPrivileges_json,
+(
+ SELECT CONCAT( -- array of array of sub-privileges
+ '[',
+ GROUP_CONCAT( -- array (json) of subprivileges
+ (
+ SELECT CONCAT(
+ '[',
+ GROUP_CONCAT(included_id),
+ ']'
+ )
+ FROM privilege_includes
+ WHERE privilege_id = privilege.id
+ )
+ ),
+ ']'
+ )
+ FROM privilege
+ WHERE privilege.id IN (
+ SELECT user_privilege.privilege_id
+ FROM user_privilege
+ WHERE user_id = 1
+ )
+) AS SubPrivileges_json
+FROM user
+WHERE id = 1
+--- */ \ No newline at end of file