Merge branch 'maint/7.0'
[ninja.git] / application / libraries / Auth.php
blobac2262862f6c6867430a4b3009ae4041c0285cb1
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');
7 /**
8 * User authentication and authorization library.
10 * @package Auth
11 * @author
12 * @copyright
13 * @license
15 class Auth {
16 /**
17 * Used to override instance, to break exception loop
19 private static $fake_instance = false;
21 /**
22 * Create an instance of Auth.
24 * @return object
26 public static function factory($config = array(), $driver_config = array())
28 Op5Auth::factory($config, $driver_config);
29 return new self();
32 /**
33 * @param $rights_to_exclude array
34 * @return 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]);
44 return $groups;
47 /**
48 * Return a static instance of Auth.
50 * @return object
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
56 try {
57 $instance = new Auth($config, $driver_config);
59 catch( Exception $e ) {
60 self::disable_auth_subsystem();
61 throw $e;
63 return $instance;
66 public function __construct( $config = NULL, $driver_config = array() )
68 Op5Auth::instance( $config, $driver_config );
71 /**
72 * Check if there is an active session. Optionally allows checking for a
73 * specific role.
75 * @param string role name
76 * @return boolean
78 public function logged_in($role = NULL)
80 if( $role !== NULL ) {
81 return false;
83 return op5auth::instance()->logged_in();
86 /**
87 * Returns the currently logged in user, or NoAuth user.
89 * @return mixed
91 public function get_user()
93 $user = op5auth::instance()->get_user();
94 return $user;
97 /**
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 );
108 return $res;
112 * Attempt to automatically log a user in.
114 * @return boolean
116 public function auto_login()
118 return false;
122 * Force a login for a specific username.
124 * @param mixed username
125 * @return boolean
127 public function force_login($username)
129 return false;
133 * Log out a user by removing the related session variables.
135 * @param boolean completely destroy the session
136 * @return boolean
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
147 * password change.
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
187 * is avalible
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();
224 } // End Auth
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
232 * page
234 class Auth_NoAuth extends Auth {
236 public function __construct($config = NULL)
240 public function logged_in($role = NULL)
242 return false;
245 public function get_user()
247 return new Op5User_NoAuth();
250 public function login($username, $password, $auth_method = false)
252 return false;
255 public function logout($destroy = FALSE)
257 return false;
260 public function verify_password( $user, $password )
262 return false;
265 public function update_password( $user, $password )
267 return false;
270 public function authorized_for( $authorization_point )
272 return false;
275 public function get_authentication_methods()
277 return false;