123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- use ChamiloSession as Session;
- class KeyAuth
- {
- const PARAM_ACCESS_TOKEN = 'access_token';
- protected static $services = array();
- public static function create_temp_token($service = null, $duration = 60, $user_id = null)
- {
- return UserApiKeyManager::create_temp_token($service, $duration, $user_id);
- }
-
- public static function get_services()
- {
- return self::$services;
- }
-
- public static function enable_services($_)
- {
- $args = func_get_args();
- $names = array();
- foreach ($args as $arg) {
- if (is_object($arg)) {
- $f = array($arg, 'get_service_name');
- $name = call_user_func($f);
- } else {
- $name = $arg;
- }
- $name = substr($name, 0, 10);
- self::$services[$name] = $name;
- }
- }
- public static function disable_services($_)
- {
- $args = func_get_args();
- $names = array();
- foreach ($args as $name) {
- $name = substr($name, 0, 10);
- unset(self::$services[$name]);
- }
- }
- public static function is_service_enabled($service)
- {
- $services = self::get_services();
- foreach ($services as $s) {
- if ($s == $service) {
- return true;
- }
- }
- return false;
- }
- public static function clear_services()
- {
- self::$services[$name] = array();
- }
-
- public static function enable()
- {
- self::enable_services(UserApiKeyManager::default_service());
- }
- public static function disable()
- {
- self::$services[$name] = array();
- }
-
- public static function is_enabled()
- {
- return !empty(self::$services);
- }
-
- public static function instance()
- {
- static $result = null;
- if (empty($result)) {
- $result = new self();
- }
- return $result;
- }
- protected function __construct()
- {
- }
-
- public function accept()
- {
-
- if (!self::is_enabled()) {
- return false;
- }
- $token = $this->get_access_token();
- if ($token->is_empty()) {
- return false;
- }
- $key = UserApiKeyManager::get_by_id($token->get_id());
- if (empty($key)) {
- return false;
- }
-
- $service = $key['api_service'];
- if (!self::is_service_enabled($service)) {
- return false;
- }
-
- $user = api_get_user_info($token->get_user_id());
- if (empty($user)) {
- return false;
- }
- if (!$user['active']) {
- return false;
- }
-
- return $token->is_valid();
- }
-
- public function login()
- {
- if (!$this->accept()) {
- return false;
- }
-
- Session::destroy();
-
- global $no_redirection, $noredirection;
- $no_redirection = true;
- $noredirection = true;
- Session::write('noredirection', $noredirection);
- $user_id = $this->get_user_id();
- $course_code = $this->get_course_code();
- $group_id = $this->get_group_id();
- Login::init_user($user_id, true);
- Login::init_course($course_code, true);
- Login::init_group($group_id, true);
- return true;
- }
-
- public function get_access_token()
- {
- $string = Request::get(self::PARAM_ACCESS_TOKEN);
- return AccessToken::parse($string);
- }
- public function get_user_id()
- {
- return $this->get_access_token()->get_user_id();
- }
- public function get_course_code()
- {
- return Request::get('cidReq', 0);
- }
- public function get_group_id()
- {
- return Request::get('gidReq', 0);
- }
- }
|