public function testApiKeyUserRole()
 {
     $user = ['name' => 'John Doe', 'first_name' => 'John', 'last_name' => 'Doe', 'email' => '*****@*****.**', 'password' => 'test1234', 'security_question' => 'Make of your first car?', 'security_answer' => 'mazda', 'is_active' => true];
     $role = ['name' => 'test_role', 'is_active' => true, 'role_service_access_by_role_id' => [['service_id' => 1, 'component' => 'config', 'verb_mask' => 1, 'requestor_mask' => 1]]];
     $this->service = ServiceHandler::getService('system');
     $rs = $this->makeRequest(Verbs::POST, 'user', [], [$user]);
     $data = $rs->getContent();
     $userId = Arr::get($data, static::$wrapper . '.0.id');
     $this->service = ServiceHandler::getService('system');
     $rs = $this->makeRequest(Verbs::POST, 'role', [], [$role]);
     $data = $rs->getContent();
     $roleId = Arr::get($data, static::$wrapper . '.0.id');
     \DreamFactory\Core\Models\UserAppRole::create(['user_id' => $userId, 'app_id' => 1, 'role_id' => $roleId]);
     $app = App::find(1);
     $apiKey = $app->api_key;
     $myUser = User::find($userId);
     $token = JWTUtilities::makeJWTByUser($myUser->id, $myUser->email);
     $this->call(Verbs::GET, '/api/v2/system', [], [], [], ['HTTP_X_DREAMFACTORY_API_KEY' => $apiKey, 'HTTP_X_DREAMFACTORY_SESSION_TOKEN' => $token]);
     $this->assertFalse(Session::isSysAdmin());
     $this->assertEquals($roleId, Session::get('role.id'));
     $rsa = Session::get('role.services');
     $this->assertTrue(!empty($rsa));
 }
Exemple #2
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;
 }