示例#1
0
 /**
  * @return string
  * @throws \DreamFactory\Core\Exceptions\UnauthorizedException
  */
 public static function refreshToken()
 {
     $token = Session::getSessionToken();
     try {
         $newToken = \JWTAuth::refresh($token);
         $payload = \JWTAuth::getPayload($newToken);
         $userId = $payload->get('user_id');
         $user = User::find($userId);
         $userInfo = $user->toArray();
         ArrayUtils::set($userInfo, 'is_sys_admin', $user->is_sys_admin);
         Session::setSessionToken($newToken);
         Session::setUserInfo($userInfo);
         static::setTokenMap($payload, $newToken);
     } catch (TokenExpiredException $e) {
         $payloadArray = \JWTAuth::manager()->getJWTProvider()->decode($token);
         $forever = boolval(ArrayUtils::get($payloadArray, 'forever'));
         if ($forever) {
             $userId = ArrayUtils::get($payloadArray, 'user_id');
             $user = User::find($userId);
             Session::setUserInfoWithJWT($user, $forever);
         } else {
             throw new UnauthorizedException($e->getMessage());
         }
     }
     return Session::getSessionToken();
 }
示例#2
0
 public function __construct($settings = [])
 {
     $verbAliases = [Verbs::PUT => Verbs::POST, Verbs::MERGE => Verbs::POST, Verbs::PATCH => Verbs::POST];
     ArrayUtils::set($settings, "verbAliases", $verbAliases);
     parent::__construct($settings);
     $this->model = \DreamFactory\Core\Models\Config::class;
 }
示例#3
0
 /**
  * @param array $settings
  */
 public function __construct($settings = [])
 {
     $verbAliases = [Verbs::PUT => Verbs::PATCH, Verbs::MERGE => Verbs::PATCH];
     ArrayUtils::set($settings, "verbAliases", $verbAliases);
     parent::__construct($settings);
     $this->model = ArrayUtils::get($settings, "model_name", $this->model);
     // could be statically set
 }
示例#4
0
 /**
  * @param array $settings
  */
 public function __construct($settings = [])
 {
     $verbAliases = [Verbs::PUT => Verbs::POST, Verbs::MERGE => Verbs::PATCH];
     ArrayUtils::set($settings, "verbAliases", $verbAliases);
     parent::__construct($settings);
     $config = ArrayUtils::get($settings, 'config');
     $this->defaultRole = ArrayUtils::get($config, 'default_role');
     $this->setDriver($config);
 }
示例#5
0
 /**
  * @param array $settings
  */
 public function __construct($settings = [])
 {
     $verbAliases = [Verbs::PUT => Verbs::POST, Verbs::MERGE => Verbs::PATCH];
     ArrayUtils::set($settings, "verbAliases", $verbAliases);
     parent::__construct($settings);
     $config = ArrayUtils::get($settings, 'config');
     $this->publicPaths = ArrayUtils::get($config, 'public_path', []);
     $this->setDriver($config);
 }
示例#6
0
文件: Role.php 项目: df-arif/df-core
 /**
  * @return array
  */
 public function getRoleServiceAccess()
 {
     $this->load('role_service_access_by_role_id', 'service_by_role_service_access');
     $rsa = $this->getRelation('role_service_access_by_role_id')->toArray();
     $services = $this->getRelation('service_by_role_service_access')->toArray();
     foreach ($rsa as $key => $s) {
         $serviceName = ArrayUtils::findByKeyValue($services, 'id', ArrayUtils::get($s, 'service_id'), 'name');
         ArrayUtils::set($rsa[$key], 'service', $serviceName);
     }
     return $rsa;
 }
示例#7
0
文件: User.php 项目: df-arif/df-user
 /**
  * {@inheritdoc}
  */
 protected function getSelectionCriteria()
 {
     $criteria = parent::getSelectionCriteria();
     $condition = ArrayUtils::get($criteria, 'condition');
     if (!empty($condition)) {
         $condition .= " AND is_sys_admin = '0'";
     } else {
         $condition = " is_sys_admin = '0'";
     }
     ArrayUtils::set($criteria, 'condition', $condition);
     return $criteria;
 }
示例#8
0
 /**
  * Sets basic info of the user in session with JWT when authenticated.
  *
  * @param  array|User $user
  * @param bool        $forever
  * @param integer     $appId
  *
  * @return bool
  */
 public static function setUserInfoWithJWT($user, $forever = false, $appId = null)
 {
     $userInfo = null;
     if ($user instanceof User) {
         $userInfo = $user->toArray();
         ArrayUtils::set($userInfo, 'is_sys_admin', $user->is_sys_admin);
     }
     if (!empty($userInfo)) {
         $id = ArrayUtils::get($userInfo, 'id');
         $email = ArrayUtils::get($userInfo, 'email');
         $token = JWTUtilities::makeJWTByUser($id, $email, $forever);
         static::setSessionToken($token);
         if (!empty($appId) && !$user->is_sys_admin) {
             static::setSessionData($appId, $id);
             return true;
         } else {
             return static::setUserInfo($userInfo);
         }
     }
     return false;
 }
示例#9
0
 /**
  * @param       $id
  * @param       $record
  * @param array $params
  *
  * @return array
  * @throws \DreamFactory\Core\Exceptions\BadRequestException
  * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException
  * @throws \DreamFactory\Core\Exceptions\NotFoundException
  */
 public static function updateInternal($id, $record, $params = [])
 {
     if (empty($record)) {
         throw new BadRequestException('There are no fields in the record to create . ');
     }
     if (empty($id)) {
         //Todo:perform logging below
         //Log::error( 'Update request with no id supplied: ' . print_r( $record, true ) );
         throw new BadRequestException('Identifying field "id" can not be empty for update request . ');
     }
     $userId = SessionUtility::getCurrentUserId();
     ArrayUtils::set($record, 'user_id', $userId);
     //Making sure name is not changed during update as it not be unique.
     ArrayUtils::set($record, 'name', $id);
     $model = static::whereUserId($userId)->whereName($id)->first();
     if (!$model instanceof Model) {
         throw new NotFoundException('No resource found for ' . $id);
     }
     $pk = $model->primaryKey;
     //	Remove the PK from the record since this is an update
     ArrayUtils::remove($record, $pk);
     try {
         $model->update($record);
         return static::buildResult($model, $params);
     } catch (\Exception $ex) {
         throw new InternalServerErrorException('Failed to update resource: ' . $ex->getMessage());
     }
 }
示例#10
0
 /**
  * Returns user info cached, or reads from db if not present.
  * Pass in a key to return a portion/index of the cached data.
  *
  * @param int         $id
  * @param null|string $key
  * @param null        $default
  *
  * @return mixed|null
  */
 public static function getCachedInfo($id, $key = null, $default = null)
 {
     $cacheKey = 'user:'******'df.default_cache_ttl'), function () use($id) {
         $user = static::with('user_lookup_by_user_id')->whereId($id)->first();
         if (empty($user)) {
             throw new NotFoundException("User not found.");
         }
         if (!$user->is_active) {
             throw new ForbiddenException("User is not active.");
         }
         $userInfo = $user->toArray();
         ArrayUtils::set($userInfo, 'is_sys_admin', $user->is_sys_admin);
         return $userInfo;
     });
     if (is_null($result)) {
         return $default;
     }
     if (is_null($key)) {
         return $result;
     }
     return isset($result[$key]) ? $result[$key] : $default;
 }
示例#11
0
 /**
  * @param array $settings
  */
 public function __construct($settings = [])
 {
     $verbAliases = [Verbs::PUT => Verbs::POST, Verbs::MERGE => Verbs::POST, Verbs::PATCH => Verbs::POST];
     ArrayUtils::set($settings, "verbAliases", $verbAliases);
     parent::__construct($settings);
 }
示例#12
0
 /**
  * Look through the known paths for a particular script. Returns full path to script file.
  *
  * @param string $name           The name/id of the script
  * @param string $path           The name of the script
  * @param bool   $returnContents If true, the contents of the file, if found, are returned. Otherwise, the only the
  *                               path is returned
  *
  * @return string
  */
 public static function loadScript($name, $path = null, $returnContents = true)
 {
     if ($path) {
         // no longer support file paths for scripts?
     }
     //  Already read, return script
     if (null !== ($script = ArrayUtils::get(static::$libraries, $name))) {
         return $returnContents ? file_get_contents($script) : $script;
     }
     $script = ltrim($script, ' /');
     //  Spin through paths and look for the script
     foreach (static::$libraryPaths as $path) {
         $check = $path . '/' . $script;
         if (is_file($check) && is_readable($check)) {
             ArrayUtils::set(static::$libraries, $name, $check);
             return $returnContents ? file_get_contents($check) : $check;
         }
     }
     return false;
 }
示例#13
0
 protected function setDriver($config)
 {
     $diskName = null;
     if (empty($config) || !isset($config['container'])) {
         $diskName = Config::get('filesystems.default');
     } else {
         $diskName = $config['container'];
     }
     if (empty($diskName)) {
         throw new InternalServerErrorException('Local file service driver/disk not configured. Please check configuration for file service - ' . $this->name . '.');
     }
     $disks = Config::get('filesystems.disks');
     if (!array_key_exists($diskName, $disks)) {
         throw new InternalServerErrorException('Local file service disk - ' . $diskName . ' not found.Please check configuration for file service - ' . $this->name . '.');
     }
     $disk = ArrayUtils::get($disks, $diskName);
     //  Replace any private lookups
     Session::replaceLookups($disk, true);
     if (!isset($disk['driver'])) {
         throw new InternalServerErrorException('Mis-configured disk - ' . $diskName . '. Driver not specified.');
     }
     switch ($disk['driver']) {
         case 'local':
             if (config('df.standalone')) {
                 $root = $disk['root'];
             } else {
                 $root = Managed::getStoragePath(config('df.local_file_service_container'));
             }
             if (!is_dir($root)) {
                 mkdir($root, 0775);
             }
             if (empty($root)) {
                 throw new InternalServerErrorException('Mis-configured disk - ' . $diskName . '. Root path not specified.');
             }
             if (!is_dir($root)) {
                 throw new InternalServerErrorException('Mis-configured disk - ' . $diskName . '. Root path not found.');
             }
             $this->driver = new LocalFileSystem($root);
             break;
         case 's3':
             $this->container = ArrayUtils::get($disk, 'bucket', ArrayUtils::get($disk, 'container'));
             ArrayUtils::set($disk, 'container', $this->container);
             if (empty($this->container)) {
                 throw new InternalServerErrorException('S3 file service bucket/container not specified. Please check configuration for file service - ' . $this->name);
             }
             $this->driver = new S3FileSystem($disk);
             break;
         case 'rackspace':
             $this->container = ArrayUtils::get($disk, 'container');
             if (empty($this->container)) {
                 throw new InternalServerErrorException('Azure blob container not specified. Please check configuration for file service - ' . $this->name);
             }
             $this->driver = new OpenStackObjectStorageSystem($disk);
             break;
         case 'azure':
             $this->container = ArrayUtils::get($disk, 'container');
             if (empty($this->container)) {
                 throw new InternalServerErrorException('Azure blob container not specified. Please check configuration for file service - ' . $this->name);
             }
             $this->driver = new AzureBlobFileSystem($disk);
             break;
         default:
             break;
     }
 }
示例#14
0
 /**
  * Removes 'config' from select criteria if supplied as it chokes the model.
  *
  * @param array $criteria
  *
  * @return array
  */
 protected static function cleanCriteria(array $criteria)
 {
     $fields = ArrayUtils::get($criteria, 'select');
     ArrayUtils::set($criteria, 'select', static::cleanFields($fields));
     return $criteria;
 }
示例#15
0
文件: Admin.php 项目: df-arif/df-core
 /**
  * Fixes supplied records to always set is_set_admin flag to true.
  * Encrypts passwords if it is supplied.
  *
  * @param array $records
  *
  * @return array
  */
 protected static function fixRecords(array $records)
 {
     if (ArrayUtils::isArrayNumeric($records)) {
         foreach ($records as $key => $record) {
             ArrayUtils::set($record, 'is_sys_admin', 1);
             $records[$key] = $record;
         }
     } else {
         ArrayUtils::set($records, 'is_sys_admin', 1);
     }
     return $records;
 }
示例#16
0
 /**
  * Selects records by multiple ids.
  *
  * @param string|array $ids
  * @param array        $related
  * @param array        $criteria
  *
  * @return mixed
  */
 public static function selectByIds($ids, array $related = [], array $criteria = [])
 {
     if (empty($criteria)) {
         $criteria['select'] = ['*'];
     }
     if (is_array($ids)) {
         $ids = implode(',', $ids);
     }
     if (!empty($ids)) {
         $pk = static::getPrimaryKeyStatic();
         $idsPhrase = " {$pk} IN ({$ids}) ";
         $condition = ArrayUtils::get($criteria, 'condition');
         if (!empty($condition)) {
             $condition .= ' AND ' . $idsPhrase;
         } else {
             $condition = $idsPhrase;
         }
         ArrayUtils::set($criteria, 'condition', $condition);
     }
     $data = static::selectByRequest($criteria, $related);
     return $data;
 }