Traq\Controllers\AppController::__construct PHP Method

__construct() public method

Always call this when defining __construct() in sub-classes.
public __construct ( )
    public function __construct()
    {
        $this->db = ConnectionManager::getConnection();
        // Modal?
        if (Request::$headers->has('X-Modal')) {
            $this->isModal = Request::$headers->get('X-Modal') == true;
        }
        // Get current project.
        if (Request::$properties->has('pslug')) {
            $this->currentProject = Project::find('slug', Request::$properties->get('pslug')) ?: null;
            $GLOBALS['current_project'] = $this->currentProject;
            $this->before('*', function () {
                if (!$this->hasPermission('view', $this->currentProject)) {
                    return $this->show404();
                }
            });
        } else {
            $GLOBALS['current_project'] = null;
        }
        // Get current user.
        if ($sessionHash = Request::$cookies->get('traq')) {
            if ($this->currentProject) {
                $user = User::select('u.*')->addSelect('pur.project_role_id')->leftJoin('u', UserRole::tableName(), 'pur', 'pur.project_id = :project_id AND pur.user_id = u.id');
                $user->where('u.session_hash = :session_hash');
                $user->setParameter('project_id', $this->currentProject['id']);
                $user->setParameter('session_hash', $sessionHash);
                $this->currentUser = $user->fetch() ?: null;
            } else {
                $this->currentUser = User::find('session_hash', $sessionHash) ?: null;
            }
            $GLOBALS['current_user'] = $this->currentUser;
        } else {
            $GLOBALS['current_user'] = null;
        }
        $GLOBALS['permissions'] = Permission::getPermissions($this->currentUser, $this->currentProject);
        // Add Traq as first breadcrumb.
        $this->addCrumb(setting('title'), $this->generateUrl('root'));
        // Check if the user has permission to view the current project
        if (isset($this->currentProject)) {
            $this->before('*', function () {
                if (!$this->hasPermission('view')) {
                    return $this->show403();
                }
            });
        }
        // If the user has a `sha1` hashed password, require them to change it because
        // as of Traq 4.1, only mcrypt passwords will work.
        if ($this->currentUser['password_ver'] == 'sha1') {
            $this->before('*', function () {
                if (Request::$properties['controller'] != 'Traq\\Controllers\\UserCP' && Request::$properties['controller'] != 'Traq\\Controllers\\Sessions') {
                    return $this->redirectTo('usercp_password');
                }
            });
        }
    }

Usage Example

Esempio n. 1
0
File: Wiki.php Progetto: nirix/traq
 public function __construct()
 {
     parent::__construct();
     $this->addCrumb($this->translate('wiki'), $this->generateUrl('wiki'));
     // Add before filter to show 404 if the wiki is disabled.
     if (!$this->currentProject->enable_wiki) {
         $this->before('*', function () {
             return $this->show404();
         });
     }
     // Get the page.
     $this->before(['revisions', 'edit', 'save', 'destroy'], function () {
         $this->page = $this->currentProject->wikiPages()->where('slug = :slug')->setParameter('slug', Request::$properties->get('slug'))->fetch();
         if (!$this->page) {
             return $this->show404();
         }
         $this->addCrumb($this->page['title'], routeUrl('wiki_page'));
     });
     // Check permissions
     $this->before(['new', 'create', 'edit', 'save', 'destroy'], function () {
         $action = Request::$properties->get('action');
         if (($action == 'new' || $action == 'create') && !$this->hasPermission('create_wiki_page')) {
             return $this->show403();
         } elseif (($action == 'edit' || $action == 'save') && !$this->hasPermission('edit_wiki_page')) {
             return $this->show403();
         } elseif ($action == 'destroy' && !$this->hasPermission('delete_wiki_page')) {
             return $this->show403();
         }
     });
 }
All Usage Examples Of Traq\Controllers\AppController::__construct