예제 #1
0
 /**
  * beforeFilter handle.
  *
  * @param Event $event The beforeFilter event that was fired.
  *
  * @return void
  */
 public function beforeFilter(Event $event)
 {
     //Define the language.
     $language = new Language($this);
     $language->setLanguage();
     //Check for the Premium.
     $premium = $this->request->session()->read('Premium.Check') ? $this->request->session()->read('Premium.Check') : null;
     if (!is_null($premium)) {
         $this->loadModel('PremiumTransactions');
         $transaction = $this->PremiumTransactions->find()->where(['txn' => $this->request->session()->read('Premium.Check'), 'user_id' => $this->request->session()->read('Auth.User.id')])->contain(['Users'])->first();
         if ($transaction) {
             //Write in the session the virtual field.
             $this->Auth->setUser($transaction->user->toArray());
             $this->request->session()->write('Auth.User.premium', $transaction->user->premium);
             $this->request->session()->delete('Premium.Check');
         }
     }
     //Set trustProxy or get the original visitor IP.
     $this->request->trustProxy = true;
     //Automatically Login.
     if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
         $this->loadModel('Users');
         $user = $this->Auth->identify();
         if ($user && $user['is_deleted'] == false) {
             $this->Auth->setUser($user);
             $user = $this->Users->newEntity($user);
             $user->isNew(false);
             $user->last_login = new Time();
             $user->last_login_ip = $this->request->clientIp();
             $this->Users->save($user);
             //Write in the session the virtual field.
             $this->request->session()->write('Auth.User.premium', $user->premium);
             //Event.
             $this->eventManager()->attach(new Badges($this));
             $user = new Event('Model.Users.register', $this, ['user' => $user]);
             $this->eventManager()->dispatch($user);
         } else {
             $this->Cookie->delete('CookieAuth');
         }
     }
     if (isset($this->request->params['prefix'])) {
         $prefix = explode('/', $this->request->params['prefix'])[0];
         switch ($prefix) {
             case 'admin':
                 $this->layout = 'admin';
                 break;
             case 'forum':
                 $this->layout = 'forum';
                 break;
         }
     }
     $allowCookies = $this->Cookie->check('allowCookies');
     $this->set(compact('allowCookies'));
 }
예제 #2
0
 /**
  * Test set language connected method with changing the language
  *
  * @return void
  */
 public function testSetLanguageConnectedAndChangeTheLanguage()
 {
     $this->controller->request->addParams(['lang' => 'en_US']);
     $this->controller->request->session()->write(['Auth' => ['User' => ['id' => 1, 'username' => 'mariano', 'avatar' => '../img/avatar.png', 'group_id' => 5, 'language' => 'fr_FR']]]);
     $language = new Language($this->controller);
     $this->assertEquals('fr_FR', $this->controller->request->session()->read('Auth.User.language'));
     $this->assertNull($this->controller->Cookie->read('language'));
     $language->setLanguage();
     $this->assertEquals('en_US', $this->controller->request->session()->read('Auth.User.language'));
     $this->assertEquals('en_US', $this->controller->Cookie->read('language'));
     $this->assertEquals('en_US', I18n::locale());
 }
예제 #3
0
파일: AppController.php 프로젝트: Xety/Xeta
 /**
  * beforeFilter handle.
  *
  * @param Event $event The beforeFilter event that was fired.
  *
  * @return void
  */
 public function beforeFilter(Event $event)
 {
     $this->loadModel('Settings');
     $this->Settings->setSettings();
     $this->Auth->config('authError', __('You need to be logged in or you are not authorized to access that location !'));
     //Define the language.
     $language = new Language($this);
     $language->setLanguage();
     //Set trustProxy to get the original visitor IP.
     $this->request->trustProxy = true;
     //Automatically Login.
     if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
         $this->loadModel('Users');
         $userLogin = $this->Auth->identify();
         if ($userLogin && $userLogin['is_deleted'] == false) {
             $this->loadComponent('TwoFactorAuth');
             //Verify if the user use 2FA and if yes, if he's authorized.
             if ($userLogin['two_factor_auth_enabled'] == true && $this->TwoFactorAuth->isAuthorized($userLogin['id']) === false) {
                 $this->Cookie->delete('CookieAuth');
             } else {
                 $this->Auth->setUser($userLogin);
                 $user = $this->Users->newEntity($userLogin, ['accessibleFields' => ['id' => true]]);
                 $user->isNew(false);
                 $user->id = $userLogin['id'];
                 $user->last_login = new Time();
                 $user->last_login_ip = $this->request->clientIp();
                 $this->Users->save($user);
                 //Badges Event.
                 $this->eventManager()->attach(new Badges($this));
                 $badge = new Event('Model.Users.register', $this, ['user' => $user]);
                 $this->eventManager()->dispatch($badge);
                 //Logs Event.
                 $this->eventManager()->attach(new Logs());
                 $event = new Event('Log.User', $this, ['user_id' => $user->id, 'username' => $user->username, 'user_ip' => $this->request->clientIp(), 'user_agent' => $this->request->header('User-Agent'), 'action' => 'user.connection.auto']);
                 $this->eventManager()->dispatch($event);
             }
         } else {
             $this->Cookie->delete('CookieAuth');
         }
     }
     //Layouts
     if (isset($this->request->params['prefix'])) {
         $prefix = explode('/', $this->request->params['prefix'])[0];
         switch ($prefix) {
             case 'admin':
                 $this->viewBuilder()->layout('admin');
                 break;
         }
     }
     $allowCookies = $this->Cookie->check('allowCookies');
     $this->set(compact('allowCookies'));
     //Site Maintenance
     if (Configure::read('Site.maintenance') === true) {
         $controller = $this->request->params['controller'];
         $action = $this->request->params['action'];
         if ($this->Auth->user()) {
             $this->loadModel('Users');
             $user = $this->Users->find()->contain(['Groups' => function ($q) {
                 return $q->select(['id', 'is_staff']);
             }])->where(['Users.id' => $this->Auth->user('id')])->first();
             if (!is_null($user) && $user->group->is_staff == true) {
                 //To prevent multiple flash messages.
                 $this->Flash->config(['clear' => true]);
                 $this->Flash->error(__("Hello {0}, The website is under maintenance, only you and the staff groups have the access !", h($user->full_name)));
             } else {
                 if (!($controller == 'Pages' && $action == 'maintenance') && !($controller == 'Users' && $action == 'login') && !($controller == 'Users' && $action == 'logout')) {
                     $this->redirect(['controller' => 'pages', 'action' => 'maintenance', 'prefix' => false]);
                 }
             }
         } else {
             if (!($controller == 'Pages' && $action == 'maintenance') && !($controller == 'Users' && $action == 'login') && !($controller == 'Users' && $action == 'logout')) {
                 $this->redirect(['controller' => 'pages', 'action' => 'maintenance', 'prefix' => false]);
             }
         }
     }
     //JavaScript Notifications.
     if ($this->request->session()->read('Notification') && !empty($this->request->session()->read('Notification'))) {
         $notification = $this->request->session()->read('Notification');
         $this->request->session()->delete('Notification');
         $this->set(compact('notification'));
     }
 }