| Conditions | 17 |
| Paths | 25 |
| Total Lines | 88 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 112 | function checkUser($userId, $userKey, $pageVisited, $SETTINGS) |
||
| 113 | { |
||
| 114 | // Should we start? |
||
| 115 | if (empty($userId) === true || empty($pageVisited) === true || empty($userKey) === true) { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Definition |
||
| 120 | $pagesRights = array( |
||
| 121 | 'user' => array( |
||
| 122 | 'home', 'items', 'search', 'kb', 'favourites', 'suggestion', 'profile', 'import', 'export', 'folders', 'offline', |
||
| 123 | ), |
||
| 124 | 'manager' => array( |
||
| 125 | 'home', 'items', 'search', 'kb', 'favourites', 'suggestion', 'folders', 'roles', 'utilities', 'users', 'profile', |
||
| 126 | 'import', 'export', 'offline', 'process', |
||
| 127 | 'utilities.deletion', 'utilities.renewal', 'utilities.database', 'utilities.logs', 'tasks', |
||
| 128 | ), |
||
| 129 | 'human_resources' => array( |
||
| 130 | 'home', 'items', 'search', 'kb', 'favourites', 'suggestion', 'folders', 'roles', 'utilities', 'users', 'profile', |
||
| 131 | 'import', 'export', 'offline', 'process', |
||
| 132 | 'utilities.deletion', 'utilities.renewal', 'utilities.database', 'utilities.logs', 'tasks', |
||
| 133 | ), |
||
| 134 | 'admin' => array( |
||
| 135 | 'home', 'items', 'search', 'kb', 'favourites', 'suggestion', 'folders', 'manage_roles', 'manage_folders', |
||
| 136 | 'import', 'export', 'offline', 'process', |
||
| 137 | 'manage_views', 'manage_users', 'manage_settings', 'manage_main', |
||
| 138 | 'admin', '2fa', 'profile', '2fa', 'api', 'backups', 'emails', 'ldap', 'special', |
||
| 139 | 'statistics', 'fields', 'options', 'views', 'roles', 'folders', 'users', 'utilities', |
||
| 140 | 'utilities.deletion', 'utilities.renewal', 'utilities.database', 'utilities.logs', 'tasks', |
||
| 141 | ), |
||
| 142 | ); |
||
| 143 | // Convert to array |
||
| 144 | $pageVisited = (is_array(json_decode($pageVisited, true)) === true) ? json_decode($pageVisited, true) : [$pageVisited]; |
||
| 145 | |||
| 146 | // Load |
||
| 147 | include_once $SETTINGS['cpassman_dir'] . '/includes/config/include.php'; |
||
| 148 | include_once $SETTINGS['cpassman_dir'] . '/includes/config/settings.php'; |
||
| 149 | |||
| 150 | // Load libraries |
||
| 151 | include_once $SETTINGS['cpassman_dir'] . '/includes/libraries/protect/SuperGlobal/SuperGlobal.php'; |
||
| 152 | $superGlobal = new protect\SuperGlobal\SuperGlobal(); |
||
| 153 | |||
| 154 | // Securize language |
||
| 155 | if ( |
||
| 156 | is_null($superGlobal->get('user_language', 'SESSION', 'user')) === true |
||
| 157 | || empty($superGlobal->get('user_language', 'SESSION', 'user')) === true |
||
| 158 | ) { |
||
| 159 | $superGlobal->put('user_language', 'english', 'SESSION', 'user'); |
||
| 160 | } |
||
| 161 | |||
| 162 | include_once $SETTINGS['cpassman_dir'] . '/includes/language/' . $superGlobal->get('user_language', 'SESSION', 'user') . '.php'; |
||
| 163 | include_once 'SplClassLoader.php'; |
||
| 164 | include_once 'main.functions.php'; |
||
| 165 | |||
| 166 | // Connect to mysql server |
||
| 167 | include_once $SETTINGS['cpassman_dir'] . '/includes/libraries/Database/Meekrodb/db.class.php'; |
||
| 168 | DB::$host = DB_HOST; |
||
| 169 | DB::$user = DB_USER; |
||
| 170 | DB::$password = defined('DB_PASSWD_CLEAR') === false ? defuseReturnDecrypted(DB_PASSWD, $SETTINGS) : DB_PASSWD_CLEAR; |
||
| 171 | DB::$dbName = DB_NAME; |
||
| 172 | DB::$port = DB_PORT; |
||
| 173 | DB::$encoding = DB_ENCODING; |
||
| 174 | DB::$ssl = DB_SSL; |
||
| 175 | DB::$connect_options = DB_CONNECT_OPTIONS; |
||
| 176 | |||
| 177 | // load user's data |
||
| 178 | $data = DB::queryfirstrow( |
||
| 179 | 'SELECT login, key_tempo, admin, gestionnaire, can_manage_all_users FROM ' . prefixTable('users') . ' WHERE id = %i', |
||
| 180 | $userId |
||
| 181 | ); |
||
| 182 | |||
| 183 | // check if user exists and tempo key is coherant |
||
| 184 | if (empty($data['login']) === true || empty($data['key_tempo']) === true || $data['key_tempo'] !== $userKey) { |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | |||
| 188 | if ( |
||
| 189 | ((int) $data['admin'] === 1 && isInArray($pageVisited, $pagesRights['admin']) === true) |
||
| 190 | || |
||
| 191 | (((int) $data['gestionnaire'] === 1 || (int) $data['can_manage_all_users'] === 1) |
||
| 192 | && (isInArray($pageVisited, array_merge($pagesRights['manager'], $pagesRights['human_resources'])) === true)) |
||
| 193 | || |
||
| 194 | (isInArray($pageVisited, $pagesRights['user']) === true) |
||
| 195 | ) { |
||
| 196 | return true; |
||
| 197 | } |
||
| 198 | |||
| 199 | return false; |
||
| 200 | } |
||
| 220 |