/** * @param tokenModel $tokenModel * * @return string * @throws tokenManagerException */ public function generateToken($tokenModel) { $tokenString = $this->encode($tokenModel->minify()); $tokenModel->setToken($tokenString); try { $tokenModel->getMapper()->save($tokenModel); } catch (\Exception $e) { throw new tokenManagerException('Unable to generate token, because sub exception was caught', 0, $e); } return $tokenString; }
/** * @param userModel|modelBase $user * * @return array * @throws \mpcmf\modules\moduleBase\exceptions\mapperException * @throws \mpcmf\system\acl\exception\aclException * @throws \mpcmf\system\application\exception\webApplicationException */ public function _token_generate($user = null) { $slim = $this->getSlim(); if ($user === null) { $user = aclManager::getInstance()->getCurrentUser(); } $tokenMapper = tokenMapper::getInstance(); if ($slim->request()->isPost() && $slim->request->post('updateToken') === 'true') { try { /** @var tokenModel $tokenModel */ $tokenModel = $tokenMapper->getBy([tokenMapper::FIELD__USER => $user->getUserId()]); } catch (mapperException $mapperException) { $tokenModel = tokenModel::fromArray([tokenMapper::FIELD__USER => $user->getUserId(), tokenMapper::FIELD__LIMIT => tokenMapper::DEFAULT_LIMIT]); } $tokenString = tokenManager::getInstance()->generateToken($tokenModel); $result = ['token' => $tokenString]; $this->getSlim()->response()->header('Content-type', 'application/json'); $this->getSlim()->response()->write(json_encode($result, JSON_UNESCAPED_UNICODE)); $this->getSlim()->stop(); } return self::nothing([]); }