protected function initialize() { $initCacheKey = 'auth/groups/init/' . environment::getCurrentEnvironment(); if (!cache::getCached($initCacheKey)) { foreach ($this->getDefaultGroups() as $defaultGroup) { if (!$this->_getBy([self::FIELD__GROUP_ALIAS => $defaultGroup[self::FIELD__GROUP_ALIAS]])) { $this->_create($defaultGroup); } } cache::setCached($initCacheKey, true); } }
protected function initialize($force = false) { $initCacheKey = 'auth/users/init/' . environment::getCurrentEnvironment(); if ($force || !cache::getCached($initCacheKey)) { foreach ($this->getDefaultUsers() as $defaultUser) { $defaultUser = $this->convertDataFromForm($defaultUser); if (!$this->_getBy([self::FIELD__LOGIN => $defaultUser[self::FIELD__LOGIN]])) { $this->_create($defaultUser); } } cache::setCached($initCacheKey, true); } }
public static function load() { if (!defined('CORE_ROOT')) { define('CORE_ROOT', __DIR__); } $loader = self::getLoader(); $map = (require APP_ROOT . '/vendor/composer/autoload_namespaces.php'); foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } $map = (require APP_ROOT . '/vendor/composer/autoload_psr4.php'); foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = (require APP_ROOT . '/vendor/composer/autoload_classmap.php'); if ($classMap) { $loader->addClassMap($classMap); } $loader->register(true); $gitFile = APP_ROOT . '/.git/ORIG_HEAD'; $gitCommit = 'default'; if (file_exists($gitFile)) { $gitCommit = trim(file_get_contents($gitFile)); } $cacheKey = "{$gitCommit}.loader"; if (!($paths = cache::getCached($cacheKey))) { $paths = ['modules' => [APP_ROOT . '/system/modules', CORE_ROOT . '/system/modules'], 'system' => []]; $appsDir = APP_ROOT . '/apps'; if (file_exists($appsDir)) { foreach (scandir($appsDir) as $appName) { if ($appName[0] === '.') { continue; } $currentModulesDir = "{$appsDir}/{$appName}/modules"; if (file_exists($currentModulesDir)) { $paths['modules'][] = $currentModulesDir; } $currentModulesSystemDir = "{$appsDir}/{$appName}/system"; if (file_exists($currentModulesSystemDir)) { $paths['system'][] = $currentModulesSystemDir; } } } cache::setCached($cacheKey, $paths); } $loader->addPsr4('mpcmf\\modules\\', $paths['modules'], false); $loader->register(false); $loader->addPsr4('mpcmf\\system\\', $paths['system'], true); $loader->addPsr4('mpcmf\\', [APP_ROOT, CORE_ROOT], true); }
public function validateToken($tokenString, $checkLimits = true) { if (empty($tokenString)) { return self::error(['errors' => ['access_token required']], self::CODE_FORBIDDEN); } $tokenData = null; if (($tokenResult = cache::getCached("token/{$tokenString}")) === false) { $tokenData = $this->decode($tokenString); $tokenResult = is_array($tokenData); cache::setCached("token/{$tokenString}", (int) $tokenResult, self::VALIDATION_EXPIRE); } if ($tokenResult === 0) { return self::error(['errors' => ['Invalid access_token given']], self::CODE_FORBIDDEN); } if ($tokenData === null) { $tokenData = $this->decode($tokenString); } return self::success($tokenData); }
/** * @param $cookieData * * @return mixed * @throws \mpcmf\modules\moduleBase\exceptions\modelException * @throws mapperException */ public function getCurrentUser($cookieData) { if (!$cookieData) { static $guestCacheKey = 'acl/user/guest'; if (!($guestData = cache::getCached($guestCacheKey))) { $guestModel = userMapper::getInstance()->getGuestUser(); /** @noinspection ReferenceMismatchInspection */ $guestData = $guestModel->export(); cache::setCached($guestCacheKey, $guestData, 3600); } else { $guestModel = userModel::fromArray($guestData); } return $guestModel; } $userId = $cookieData[userMapper::FIELD__USER_ID]; $userCacheKey = "acl/user/{$userId}"; if (!($userData = cache::getCached($userCacheKey))) { $userModel = userMapper::getInstance()->getById($userId); /** @noinspection ReferenceMismatchInspection */ $userData = $userModel->export(); cache::setCached($userCacheKey, $userData, 300); } else { $userModel = userModel::fromArray($userData); } return $userModel; }
private function registerActionsGroup() { $entityName = ltrim($this->getEntityUniqueName(), '/'); $entityActions = array_keys($this->actions); $groupsListHash = md5($entityName . implode(',', $entityActions)); $cacheKey = "action/groups/{$entityName}/{$groupsListHash}"; MPCMF_LL_DEBUG && self::log()->addDebug("[{$entityName}] Checking action's groups registration... (key: {$cacheKey})"); if (!cache::getCached($cacheKey)) { MPCMF_LL_DEBUG && self::log()->addDebug("[{$entityName}] Cached not found, building groups..."); $entityAclGroups = []; /** @var action $actionData */ foreach ($entityActions as $actionName) { $entityAclGroups[] = "{$entityName}/{$actionName}"; } MPCMF_DEBUG && self::log()->addDebug("[{$entityName}] Registering action's groups: " . count($entityAclGroups)); MPCMF_LL_DEBUG && self::log()->addDebug("[{$entityName}] Groups: " . implode(',', $entityAclGroups)); aclManager::getInstance()->createGroupsByList($entityAclGroups); cache::setCached($cacheKey, true); } else { MPCMF_LL_DEBUG && self::log()->addDebug("[{$entityName}] Found registered groups, skipping"); } }
public function _passwordRecovery() { $loginFields = ['email' => userMapper::FIELD__EMAIL]; $mapper = $this->getMapper(); $slim = $this->getSlim(); if ($slim->request()->isPost()) { $item = $slim->request()->post('item'); try { /** @var userModel $userModel */ $userModel = $this->getMapper()->getBy([$loginFields['email'] => $item[$loginFields['email']]]); $cacheKey = "passwordRecovery/{$userModel->getIdValue()}"; if (cache::getCached($cacheKey)) { return self::error(['message' => 'You exceed limit of pass recovery, come back tomorrow', 'loginFields' => $loginFields]); } $newPassword = $this->generatePassword(); $passwordHash = $mapper->convert(userMapper::FIELD__PASSWORD, $newPassword); $userModel->setPassword($passwordHash); $mapper->save($userModel); cache::setCached($cacheKey, true, 86400); } catch (mapperException $mapperException) { return self::error(['message' => 'Something going wrong, try later', 'errors' => [$mapperException->getMessage()], 'loginFields' => $loginFields]); } $email = $userModel->getEmail(); $body = <<<BODY <p>Dear {$userModel->getFirstName()}!</p> <p>Somebody start password recovery procedure</p> <p>Now, your password was changed to</p> <p><b>{$newPassword}</b></p> <p>You can login with this pass and change it in your profile</p> <p>Your SDS</p> BODY; $this->sendMail([$email], 'Password recovery', $body); return self::success([]); } return self::nothing(['loginFields' => $loginFields]); }
public function getMenuStructure($full = false, $reallyFull = false) { $aclManager = aclManager::getInstance(); $groups = $aclManager->getCurrentUser()->getGroupIds(); $cacheKey = 'webApp/sidebar/menu/' . md5(json_encode($groups)); if (!($menu = cache::getCached($cacheKey))) { $homeMenuItem = ['path' => '/', 'name' => i18n::lang()->get('Главная')]; $menu = [$homeMenuItem]; foreach ($this->getAllModules() as $moduleName => $module) { $modulePath = "/{$moduleName}"; $menuItem = ['path' => $modulePath, 'name' => $module->getName(), 'subitems' => []]; $hasSubItemsAccess = false; /** * @var string $entityUniqueName * @var array $entity */ foreach ($module->getModuleRoutes()->getStructure() as $entityUniqueName => $entityData) { /** @var modelBase $entityModel */ $entityModel = $entityData['entity']; $subMenuItem = ['path' => $entityUniqueName, 'name' => $entityModel->getPublicName(), 'subitems' => []]; $hasSubSubItemsAccess = false; /** * @var string $actionName * @var action $actionModel */ foreach ($entityData['actions'] as $actionName => $actionModel) { if (!$reallyFull && $actionModel->getType() !== action::TYPE__GLOBAL) { continue; } $actionPath = preg_replace('/\\(.*\\)/', '', $actionModel->getPath()); if ($actionModel->isRelative()) { $path = $actionModel->getActionUniqueName() . $actionPath; } else { $path = '/' . ltrim($actionPath, '/'); } $hasAccess = $aclManager->checkActionAccess($actionModel)['status']; if (!$hasSubSubItemsAccess && $hasAccess) { $hasSubSubItemsAccess = true; } if ($full || $reallyFull || $hasAccess) { $subSubMenuItem = ['path' => $path, 'name' => $actionModel->getName(), 'access' => $hasAccess]; $subMenuItem['subitems'][] = $subSubMenuItem; } } $subMenuItem['hasSubItemsAccess'] = $hasSubSubItemsAccess; if (!$hasSubItemsAccess && $hasSubSubItemsAccess) { $hasSubItemsAccess = true; } if ($full || $reallyFull || $hasSubSubItemsAccess) { $menuItem['subitems'][] = $subMenuItem; } } $menuItem['hasSubItemsAccess'] = $hasSubItemsAccess; if ($full || $reallyFull || $hasSubItemsAccess) { $menu[] = $menuItem; } } cache::setCached($cacheKey, $menu, 300); } $slim = $this->slim(); $currentRoutePath = $slim->urlFor($slim->router()->getCurrentRoute()->getName()); $this->processMenuItems($menu, $currentRoutePath); return $menu; }
/** * * @return array|null|void * @throws mapperException * @throws modulePartsHelperException */ public function api_getInfo() { $countCacheKey = $this->getCurrentClassName() . '/count/' . md5(date('Y-m-d_H-i')); try { $cursor = $this->getMapper()->getAllBy(); } catch (mapperException $mapperException) { return ['response' => self::errorByException($mapperException)]; } catch (modulePartsHelperException $modulePartsHelperException) { return ['response' => self::errorByException($modulePartsHelperException)]; } $mongoCursor = $cursor->getCursor(); if (($count = cache::getCached($countCacheKey)) === false) { $count = $mongoCursor->count(); cache::setCached($countCacheKey, $count, 60); } return ['response' => self::success(['identity' => ['module' => $this->getModuleName(), 'entity' => $this->getEntityName(), 'entityUniqueName' => $this->getEntityUniqueName(), 'publicName' => $this->getPublicName()], 'actions' => array_keys($this->getEntityActions()->getActions()), 'count' => $count])]; }