1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 require_once('op5/auth/Auth.php');
4 require_once('op5/auth/User_NoAuth.php');
5 require_once('op5/auth/User_AlwaysAuth.php');
8 * User authentication and authorization library.
17 * Used to override instance, to break exception loop
19 private static $fake_instance = false;
22 * Create an instance of Auth.
26 public static function factory($config = array(), $driver_config = array())
28 Op5Auth
::factory($config, $driver_config);
33 * @param $rights_to_exclude array
36 public static function get_groups_without_rights(array $rights_to_exclude)
38 $groups = Op5Config
::instance()->getConfig('auth_groups');
39 foreach($groups as $group => $rights) {
40 if(array_intersect($rights_to_exclude, $rights)) {
41 unset($groups[$group]);
48 * Return a static instance of Auth.
52 public static function instance($config = array(), $driver_config = array())
54 if (self
::$fake_instance !== false) return self
::$fake_instance;
55 // Load the Auth instance
57 $instance = new Auth($config, $driver_config);
59 catch( Exception
$e ) {
60 self
::disable_auth_subsystem();
66 public function __construct( $config = NULL, $driver_config = array() )
68 Op5Auth
::instance( $config, $driver_config );
72 * Check if there is an active session. Optionally allows checking for a
75 * @param string role name
78 public function logged_in($role = NULL)
80 if( $role !== NULL ) {
83 return op5auth
::instance()->logged_in();
87 * Returns the currently logged in user, or NoAuth user.
91 public function get_user()
93 $user = op5auth
::instance()->get_user();
98 * Attempt to log in a user by using an ORM object and plain-text password.
100 * @param string username to log in
101 * @param string password to check against
102 * @param boolean enable auto-login
103 * @return boolean True on success
105 public function login($username, $password, $auth_method = false)
107 $res = op5auth
::instance()->login( $username, $password, $auth_method );
112 * Attempt to automatically log a user in.
116 public function auto_login()
122 * Force a login for a specific username.
124 * @param mixed username
127 public function force_login($username)
133 * Log out a user by removing the related session variables.
135 * @param boolean completely destroy the session
138 public function logout($destroy = FALSE)
140 return op5auth
::instance()->logout();
144 * Verify password for a logged in user.
146 * Usable for form validation of critical user data, for example validate a
149 * This method doesn't use APC
151 * @param $user Op5User User object to verify
152 * @param $password string Password to test
153 * @return boolean true if password is ok
155 public function verify_password( $user, $password )
157 return op5auth
::instance()->verify_password( $user, $password );
161 * Update password for a given user.
163 * @param $user Op5User User object to verify
164 * @param $password string New password
165 * @return boolean true if password is ok
167 public function update_password( $user, $password )
169 return op5auth
::instance()->update_password( $user, $password );
173 * Returns true if current session has access for a given authorization point
175 * @param string authorization point
176 * @return boolean true if access
178 public function authorized_for( $authorization_point )
180 return op5auth
::instance()->authorized_for( $authorization_point );
184 * Returns an array of authentication methods.
186 * @return array list of authentication methods, or false if only a single
189 public function get_authentication_methods()
191 return op5auth
::instance()->get_authentication_methods();
195 * Returns name of default authentication method.
197 * @return string default authentication method
200 public function get_default_auth()
202 return op5auth
::instance()->get_default_auth();
206 * Take an op5User object, and force the auth module to recognize it as the
207 * currently logged in user
209 public function force_user($user)
211 return op5auth
::instance()->force_user($user);
215 * Register noauth as auth subsystem, so we can't login, logout or anything.
217 * This effectivly reduces possibilities for auth-related errors. Because lot
218 * of things depend on auth, even when rendering, this is needed to be loaded
219 * when displaying error pages.
221 public static function disable_auth_subsystem() {
222 self
::$fake_instance = new Auth_NoAuth();
228 * This class is just to fill in as Auth if exception if thrown in factory.
230 * When showing an error page (as from exception in factory method), the instance
231 * needs to be set, so not a new exception will be thrown when rendering the error
234 class Auth_NoAuth
extends Auth
{
236 public function __construct($config = NULL)
240 public function logged_in($role = NULL)
245 public function get_user()
247 return new Op5User_NoAuth();
250 public function login($username, $password, $auth_method = false)
255 public function logout($destroy = FALSE)
260 public function verify_password( $user, $password )
265 public function update_password( $user, $password )
270 public function authorized_for( $authorization_point )
275 public function get_authentication_methods()