Beispiel #1
0
 public function afterLogin($event, $loginUser)
 {
     if (!$loginUser->id) {
         return;
     }
     $storage = Login::getAuthStorage();
     if (Login::getLoginMode() == Login::LOGIN_MODE_TOKEN) {
         $apikey = new Apikey();
         $userId = $loginUser->id;
         $token = $apikey->findFirst("userId = {$userId}");
         if (!$token) {
             $token = $apikey->generateToken($userId);
         }
         $storage->setId($token->apikey);
         $storage->set(Login::AUTH_KEY_TOKEN, $token);
     }
     $defaultRoles = $loginUser->getRoles();
     $roles = $loginUser->roles;
     $authRoles = array();
     if ($roles) {
         foreach ($roles as $role) {
             $authRoles[] = $role->roleKey;
         }
     }
     $authRoles = array_unique(array_merge($defaultRoles, $authRoles));
     $storage->set(Login::AUTH_KEY_ROLES, $authRoles);
 }
Beispiel #2
0
 public function getToken()
 {
     if ($this->token) {
         return $this->token;
     }
     $token = new Apikey();
     $token->setToken($this->apikey);
     return $this->token = $token;
 }
Beispiel #3
0
 public function generateToken($userId)
 {
     $plan = $this->getDI()->getConfig->permission->keyLevels->basic;
     $apikey = new Apikey();
     $apikey->userId = $userId;
     $apikey->level = 'basic';
     $apikey->minutelyRate = $plan->minutelyRate;
     $apikey->hourlyRate = $plan->hourlyRate;
     $apikey->dailyRate = $plan->dailyRate;
     if (!$apikey->save()) {
         throw new Exception\RuntimeException('ERR_PERMISSION_APIKEY_GENERATE_FAILED');
     }
     return $apikey;
 }
Beispiel #4
0
 /**
  * @operationName("Remove API key")
  * @operationDescription("Remove API key")
  */
 public function deleteAction()
 {
     $this->response->setContentType('application/json', 'utf-8');
     if (!$this->request->isDelete()) {
         $this->response->setStatusCode('405', 'Method Not Allowed');
         return $this->response->setJsonContent(array('errors' => array(array('code' => 405, 'message' => 'ERR_POST_REQUEST_METHOD_NOT_ALLOW'))));
     }
     $id = $this->dispatcher->getParam('id');
     $apikey = Models\Apikey::findFirst($id);
     try {
         $apikey->delete();
     } catch (\Exception $e) {
         return $this->showExceptionAsJson($e, $apikey->getMessages());
     }
     return $this->response->setJsonContent($apikey);
 }