<?php
 namespace Extension;
 use Cookie;
 use TCMS\HookExtension;
 class ChocolaHookExt extends HookExtension {
 public function startup() {
 include_once(CONFIG_PATH . 'role.php');
 }
 public function beforeModuleLoad($module) {
 if($module->authExtension === null && full_url() != url('/login')) {
 $auth = load_auth();
 if($auth) {
 $user = $auth->auth();
 if(!$user) {
 header("Location: " . $auth->loginUrl . '?redirect_url='.full_url());
 exit();
 }
 else {
 if($module->middleware(route_name(), $auth) === false) {
 if(\RoleGlobal::$role->lastRoleCalled) {
 $r = \RoleGlobal::$role->getRole(\RoleGlobal::$role->lastRoleCalled);
 throw new \TCMS\TException(lang('unauthorized.authorized'). ' ' . $r->name . ' ' . $r->group, 401);
 }
 else {
 if(strpos(route_name(), 'index') !== false) throw new \TCMS\TException(lang('unauthorized.view'), 401);
 else if(strpos(route_name(), 'create') !== false) throw new \TCMS\TException(lang('unauthorized.create'), 401);
 else if(strpos(route_name(), 'update') !== false) throw new \TCMS\TException(lang('unauthorized.update'), 401);
 else if(strpos(route_name(), 'delete') !== false) throw new \TCMS\TException(lang('unauthorized.delete'), 401);
 else throw new \TCMS\TException(lang('unauthorized.unauthorized'), 401);
 }
 }
 }
 }
 }
 }
 public function beforeControllerLoad($controller) {
 }
 public function beforeViewLoad($view) {
 }
 }
