| 1: | <?php |
| 2: | /** |
| 3: | * This file is part of the PHPLucidFrame library. |
| 4: | * Core utility for user authentication system |
| 5: | * |
| 6: | * @package PHPLucidFrame\Core |
| 7: | * @since PHPLucidFrame v 1.0.0 |
| 8: | * @copyright Copyright (c), PHPLucidFrame. |
| 9: | * @link http://phplucidframe.com |
| 10: | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 11: | * |
| 12: | * This source file is subject to the MIT license that is bundled |
| 13: | * with this source code in the file LICENSE |
| 14: | */ |
| 15: | |
| 16: | /** |
| 17: | * Check and get the authentication configuration settings |
| 18: | */ |
| 19: | function auth_prerequisite() |
| 20: | { |
| 21: | db_prerequisite(); |
| 22: | |
| 23: | $auth = _cfg('auth'); |
| 24: | |
| 25: | if (isset($auth['table']) && $auth['table'] && |
| 26: | isset($auth['fields']['id']) && $auth['fields']['id'] && |
| 27: | isset($auth['fields']['role']) && $auth['fields']['role']) { |
| 28: | return $auth; |
| 29: | } else { |
| 30: | _header(400); |
| 31: | throw new \InvalidArgumentException('Required to configure <code class="inline">$lc_auth</code> in <code class="inline">/inc/config.php</code>.'); |
| 32: | } |
| 33: | } |
| 34: | |
| 35: | if (!function_exists('auth_create')) { |
| 36: | /** |
| 37: | * Create Authentication object |
| 38: | * This function is overridable from the custom helpers/auth_helper.php |
| 39: | * |
| 40: | * @param string $id PK value |
| 41: | * @param object $data The user data object (optional). If it is not given, auth_create will load it from db |
| 42: | * |
| 43: | * @return object|bool The authenticated user object or FALSE on failure |
| 44: | */ |
| 45: | function auth_create($id, $data = null) |
| 46: | { |
| 47: | $lc_auth = auth_prerequisite(); |
| 48: | $auth = auth_get(); |
| 49: | |
| 50: | if (!$auth) { |
| 51: | $session = is_object($data) ? $data : auth_getUserInfo($id); |
| 52: | if (isset($session)) { |
| 53: | $fieldRole = $lc_auth['fields']['role']; |
| 54: | |
| 55: | $session->sessId = session_id(); |
| 56: | $session->timestamp = time(); |
| 57: | $session->token = strtoupper(_randomCode(20)); |
| 58: | $session->permissions = auth_permissions($session->$fieldRole); |
| 59: | |
| 60: | auth_set($session); |
| 61: | |
| 62: | return $session; |
| 63: | } |
| 64: | } else { |
| 65: | return $auth; |
| 66: | } |
| 67: | |
| 68: | return false; |
| 69: | } |
| 70: | } |
| 71: | |
| 72: | if (!function_exists('auth_getUserInfo')) { |
| 73: | /** |
| 74: | * Get user record from db to create auth session |
| 75: | * This function is overridable from the custom helpers/auth_helper.php |
| 76: | * @param int $id User ID |
| 77: | * @return mixed |
| 78: | */ |
| 79: | function auth_getUserInfo($id) |
| 80: | { |
| 81: | $auth = _cfg('auth'); |
| 82: | $table = db_table($auth['table']); |
| 83: | $fieldId = $auth['fields']['id']; |
| 84: | |
| 85: | return db_select($table) |
| 86: | ->where()->condition($fieldId, $id) |
| 87: | ->getSingleResult(); |
| 88: | } |
| 89: | } |
| 90: | |
| 91: | /** |
| 92: | * Get the namespace for the authentication object |
| 93: | * The Auth session name can be different upon directory (namespace) |
| 94: | * But it can also be shared according to $lc_sharedNamespaces |
| 95: | * |
| 96: | * @return string |
| 97: | */ |
| 98: | function auth_namespace() |
| 99: | { |
| 100: | $sites = _cfg('sites'); |
| 101: | $namespaces = _cfg('sharedNamespaces'); |
| 102: | |
| 103: | if (LC_NAMESPACE && isset($sites[LC_NAMESPACE]) && isset($namespaces[LC_NAMESPACE])) { |
| 104: | $namespace = $namespaces[LC_NAMESPACE]; |
| 105: | } else { |
| 106: | $namespace = LC_NAMESPACE; |
| 107: | } |
| 108: | |
| 109: | return LC_NAMESPACE ? 'AuthUser.' . $namespace : 'AuthUser.default'; |
| 110: | } |
| 111: | |
| 112: | /** |
| 113: | * Get the authenticated user object from Session |
| 114: | * @return mixed |
| 115: | */ |
| 116: | function auth_get() |
| 117: | { |
| 118: | return session_get(auth_namespace(), true); |
| 119: | } |
| 120: | |
| 121: | /** |
| 122: | * Set the authenticated user object to Session |
| 123: | * @param object $sess The authentication object |
| 124: | */ |
| 125: | function auth_set($sess) |
| 126: | { |
| 127: | _app('auth', $sess); |
| 128: | session_set(auth_namespace(), $sess, true); |
| 129: | } |
| 130: | |
| 131: | /** |
| 132: | * Clear the authenticated user object from session |
| 133: | */ |
| 134: | function auth_clear() |
| 135: | { |
| 136: | session_delete(auth_namespace()); |
| 137: | _app('auth', null); |
| 138: | } |
| 139: | |
| 140: | /** |
| 141: | * Check if a user is not authenticated |
| 142: | * @return bool TRUE if user is not authenticated, otherwise FALSE |
| 143: | */ |
| 144: | function auth_isAnonymous() |
| 145: | { |
| 146: | $auth = auth_prerequisite(); |
| 147: | $field = $auth['fields']['id']; |
| 148: | $session = auth_get(); |
| 149: | |
| 150: | return (is_object($session) && $session->$field > 0) ? false : true; |
| 151: | } |
| 152: | |
| 153: | /** |
| 154: | * Check if a user is authenticated |
| 155: | * @return boolean |
| 156: | */ |
| 157: | function auth_isLoggedIn() |
| 158: | { |
| 159: | return ! auth_isAnonymous(); |
| 160: | } |
| 161: | |
| 162: | if (!function_exists('auth_permissions')) { |
| 163: | /** |
| 164: | * Get the permissions of a particular role |
| 165: | * This function is overridable from the custom helpers/auth_helper.php |
| 166: | * @param string $role The user role name or id |
| 167: | * @return array|null Array of permissions of the role |
| 168: | */ |
| 169: | function auth_permissions($role) |
| 170: | { |
| 171: | $auth = _cfg('auth'); |
| 172: | $perms = isset($auth['permissions']) ? $auth['permissions'] : array(); |
| 173: | |
| 174: | return isset($perms[$role]) ? $perms[$role] : null; |
| 175: | } |
| 176: | } |
| 177: | |
| 178: | if (!function_exists('auth_role')) { |
| 179: | /** |
| 180: | * Check if the authenticated user has the specific user role |
| 181: | * This function is overridable from the custom helpers/auth_helper.php |
| 182: | * @param string $role The user role name or id |
| 183: | * @return boolean |
| 184: | */ |
| 185: | function auth_role($role) |
| 186: | { |
| 187: | if (auth_isAnonymous()) { |
| 188: | return false; |
| 189: | } |
| 190: | |
| 191: | $auth = auth_prerequisite(); |
| 192: | $field = $auth['fields']['role']; |
| 193: | $session = auth_get(); |
| 194: | |
| 195: | return $session->$field == $role; |
| 196: | } |
| 197: | } |
| 198: | |
| 199: | if (!function_exists('auth_roles')) { |
| 200: | /** |
| 201: | * Check if the authenticated user has the specific user role(s) |
| 202: | * This function is overridable from the custom helpers/auth_helper.php |
| 203: | * @param string [$role, ...] The list of user role names |
| 204: | * @return boolean |
| 205: | */ |
| 206: | function auth_roles() |
| 207: | { |
| 208: | if (auth_isAnonymous()) { |
| 209: | return false; |
| 210: | } |
| 211: | |
| 212: | $auth = auth_prerequisite(); |
| 213: | $field = $auth['fields']['role']; |
| 214: | $session = auth_get(); |
| 215: | $roles = func_get_args(); |
| 216: | |
| 217: | return in_array($session->$field, $roles); |
| 218: | } |
| 219: | } |
| 220: | |
| 221: | if (!function_exists('auth_can')) { |
| 222: | /** |
| 223: | * Check if the authenticated user has a particular permission |
| 224: | * This function is overridable from the custom helpers/auth_helper.php |
| 225: | * @param string $perm The permission name |
| 226: | * @return boolean TRUE if the authenticated user has a particular permission, otherwise FALSE |
| 227: | */ |
| 228: | function auth_can($perm) |
| 229: | { |
| 230: | if (auth_isAnonymous()) { |
| 231: | return false; |
| 232: | } |
| 233: | |
| 234: | $sess = auth_get(); |
| 235: | |
| 236: | if (!is_array($sess->permissions)) { |
| 237: | return false; |
| 238: | } |
| 239: | |
| 240: | if (count($sess->permissions) == 0 || in_array($perm, $sess->permissions)) { |
| 241: | return true; |
| 242: | } |
| 243: | |
| 244: | return false; |
| 245: | } |
| 246: | } |
| 247: |