Beispiel #1
0
 public static function getAllSessionRoles()
 {
     $sessionRoleLabels = array();
     $sessionRoles = array();
     $interface = new InterfaceObject('SessionRoles');
     $session = new Atom(session_id(), 'SESSION');
     $sessionRoleLabels = array_keys((array) $session->getContent($interface, true));
     foreach (Role::getAllRoleObjects() as $role) {
         if (in_array($role->label, $sessionRoleLabels) || $role->id == 0) {
             $sessionRoles[] = $role;
         }
     }
     return $sessionRoles;
 }
Beispiel #2
0
 /**
  * @url GET resource/{concept}/{srcAtomId}/{interfaceId}
  * @url GET resource/{concept}/{srcAtomId}/{interfaceId}/{tgtAtomId}
  * @param string $concept
  * @param string $srcAtomId
  * @param string $interfaceId
  * @param string $tgtAtomId
  * @param array $roleIds
  * @param boolean $inclLinktoData
  * @param string $arrayType
  * @param boolean $metaData
  */
 public function getAtom($concept, $srcAtomId, $interfaceId, $tgtAtomId = null, $roleIds = null, $inclLinktoData = false, $arrayType = "assoc", $metaData = true)
 {
     try {
         $session = Session::singleton();
         $session->activateRoles($roleIds);
         $session->setInterface($interfaceId);
         $result = array();
         if ($session->interface->srcConcept != $concept) {
             throw new Exception("Concept '{$concept}' cannot be used as source concept for interface '" . $session->interface->label . "'", 400);
         }
         if (!$session->interface->crudR) {
             throw new Exception("GET is not allowed for interface " . $session->interface->label, 405);
         }
         $atom = new Atom($srcAtomId, $concept);
         if (!$atom->atomExists()) {
             throw new Exception("Resource '{$srcAtomId}' not found", 404);
         }
         $result = (array) $atom->getContent($session->interface, true, $tgtAtomId, $inclLinktoData, $arrayType, $metaData);
         if (empty($result)) {
             Notifications::addInfo("No results found");
         }
         if (is_null($tgtAtomId)) {
             // return array of atoms (i.e. tgtAtoms of the interface given srcAtomId)
             return array_values($result);
             // array_values transforms assoc array to non-assoc array
         } else {
             // return 1 atom (i.e. tgtAtomId)
             return current($result);
         }
     } catch (Exception $e) {
         throw new RestException($e->getCode(), $e->getMessage());
     }
 }
Beispiel #3
0
 public function getContent($interface, $rootElement = true, $tgtAtom = null, $inclLinktoData = false, $arrayType = "assoc", $metaData = true, $recursionAtomArr = array())
 {
     $session = Session::singleton();
     if (is_null($tgtAtom)) {
         $idEsc = $this->database->escape($this->id);
         $query = "SELECT DISTINCT `tgt` FROM ({$interface->expressionSQL}) AS `results` WHERE `src` = '{$idEsc}' AND `tgt` IS NOT NULL";
         $tgtAtoms = array_column($this->database->Exe($query), 'tgt');
     } else {
         // Make sure that atom is in db (not necessarily the case: e.g. new atom)
         $this->database->addAtomToConcept($this->id, $this->concept);
         $tgtAtoms[] = $tgtAtom;
     }
     foreach ($tgtAtoms as $tgtAtomId) {
         $tgtAtom = new Atom($tgtAtomId, $interface->tgtConcept, $interface->viewId);
         // Add @context for JSON-LD to rootElement
         if ($rootElement) {
             $content['@context'] = Config::get('serverURL') . Config::get('apiPath') . '/interface/' . $interface->id;
         }
         // Leaf
         if (empty($interface->subInterfaces) && empty($interface->refInterfaceId)) {
             // Property
             if ($interface->isProperty && !$interface->isIdent) {
                 $content = !is_null($tgtAtom->id);
                 // convert NULL into false and everything else in true
                 // Object
             } elseif ($interface->tgtConceptIsObject) {
                 $content = array();
                 // Add meta data
                 if ($metaData) {
                     // Define interface(s) to navigate to for this tgtAtom
                     $atomInterfaces = array();
                     if ($interface->isLinkTo && !$inclLinktoData && $session->role->isInterfaceForRole($interface->refInterfaceId)) {
                         $atomInterfaces[] = array('id' => $interface->refInterfaceId, 'label' => $interface->refInterfaceId);
                     } elseif (isset($session->role)) {
                         $atomInterfaces = array_map(function ($o) {
                             return array('id' => $o->id, 'label' => $o->label);
                         }, $session->role->getInterfacesToReadConcept($interface->tgtConcept));
                     }
                     // Add meta data elements
                     $content = array_merge($content, array('@id' => $tgtAtom->jsonld_id, '@label' => $tgtAtom->label, '@view' => $tgtAtom->view, '@type' => $tgtAtom->jsonld_type, '@interfaces' => $atomInterfaces, '_sortValues_' => array()));
                 }
                 // Add id TODO:can be removed when angular templates use @id instead of id
                 $content = array_merge($content, array('id' => $tgtAtom->id));
                 // Scalar
             } else {
                 $content = $this->typeConversion($tgtAtom->id, $interface->tgtConcept);
                 // TODO: now same conversion as to database is used, maybe this must be changed to JSON types (or the json_encode/decode does this automaticaly?)
             }
             // Tree
         } else {
             $content = array();
             // Add meta data
             if ($metaData) {
                 // Define interface(s) to navigate to for this tgtAtom
                 $atomInterfaces = array();
                 if ($interface->isLinkTo && !$inclLinktoData && $session->role->isInterfaceForRole($interface->refInterfaceId)) {
                     $atomInterfaces[] = array('id' => $interface->refInterfaceId, 'label' => $interface->refInterfaceId);
                 } elseif (isset($session->role)) {
                     $atomInterfaces = array_map(function ($o) {
                         return array('id' => $o->id, 'label' => $o->label);
                     }, $session->role->getInterfacesToReadConcept($interface->tgtConcept));
                 }
                 // Add meta data elements
                 $content = array_merge($content, array('@id' => $tgtAtom->jsonld_id, '@label' => $tgtAtom->label, '@view' => $tgtAtom->view, '@type' => $tgtAtom->jsonld_type, '@interfaces' => $atomInterfaces, '_sortValues_' => array()));
             }
             // Add id TODO:can be removed when angular templates use @id instead of id
             $content = array_merge($content, array('id' => $tgtAtom->id));
             // Subinterfaces
             if (!empty($interface->subInterfaces)) {
                 if (!$interface->tgtConceptIsObject) {
                     throw new Exception("TgtConcept of interface: '" . $interface->label . "' is scalar and can not have subinterfaces", 501);
                 }
                 foreach ($interface->subInterfaces as $subinterface) {
                     $otherAtom = $tgtAtom->getContent($subinterface, false, null, $inclLinktoData, $arrayType, $metaData);
                     $content[$subinterface->id] = $otherAtom;
                     // _sortValues_ (if subInterface is uni)
                     if ($subinterface->univalent && $metaData) {
                         // property
                         if (is_bool($otherAtom)) {
                             $content['_sortValues_'][$subinterface->id] = $otherAtom;
                         } elseif ($subinterface->tgtConceptIsObject) {
                             $content['_sortValues_'][$subinterface->id] = current((array) $otherAtom)['@label'];
                         } else {
                             $content['_sortValues_'][$subinterface->id] = $otherAtom;
                         }
                     }
                 }
             }
             // Ref subinterfaces (for LINKTO interfaces only when $inclLinktoData = true)
             if (!empty($interface->refInterfaceId) && (!$interface->isLinkTo || $inclLinktoData) && $recursionAtomArr[$tgtAtom->id] < 2) {
                 if (!$interface->tgtConceptIsObject) {
                     throw new Exception("TgtConcept of interface: '" . $interface->label . "' is scalar and can not have a ref interface defined", 501);
                 }
                 if ($inclLinktoData) {
                     $recursionAtomArr[$tgtAtom->id]++;
                 }
                 $refInterface = new InterfaceObject($interface->refInterfaceId, null);
                 foreach ($refInterface->subInterfaces as $subinterface) {
                     $otherAtom = $tgtAtom->getContent($subinterface, false, null, $inclLinktoData, $arrayType, $metaData, $recursionAtomArr);
                     $content[$subinterface->id] = $otherAtom;
                     // _sortValues_ (if subInterface is uni)
                     if ($subinterface->univalent && $metaData) {
                         // property
                         if (is_bool($otherAtom)) {
                             $content['_sortValues_'][$subinterface->id] = $otherAtom;
                         } elseif ($subinterface->tgtConceptIsObject) {
                             $content['_sortValues_'][$subinterface->id] = current((array) $otherAtom)['@label'];
                         } else {
                             $content['_sortValues_'][$subinterface->id] = $otherAtom;
                         }
                     }
                 }
             }
         }
         // Determine whether value of atom must be inserted as list or as single value
         // Properties are represented as single value
         if ($interface->isProperty && !$interface->isIdent && empty($interface->subInterfaces) && empty($interface->refInterfaceId)) {
             $arr = $content;
             // Object are always inserted as array
         } elseif ($interface->tgtConceptIsObject) {
             switch ($arrayType) {
                 case "num":
                     if ($interface->univalent && !$rootElement) {
                         $arr = $content;
                     } else {
                         $arr[] = $content;
                     }
                     break;
                 case "assoc":
                 default:
                     $arr[$content['id']] = $content;
                     break;
             }
             // Non-object UNI results are inserted as single value
         } elseif ($interface->univalent) {
             $arr = $content;
             // Non-object Non-UNI results are inserted as array
         } else {
             $arr[] = $content;
         }
         unset($content);
     }
     return $arr;
 }
Beispiel #4
0
 public static function getSessionVars()
 {
     if (!Config::get('loginEnabled')) {
         return false;
     } else {
         try {
             $ifc = new InterfaceObject('SessionVars');
             $session = new Atom(session_id(), 'SESSION');
             return $session->getContent($ifc, false, null, false, 'num', false);
             // $rootElement = false => this will return a single object instead of array.
         } catch (Exception $e) {
             return false;
         }
     }
 }
 private function callback($code, $idp)
 {
     try {
         $identityProviders = Config::get('identityProviders', 'OAuthLogin');
         if (empty($code)) {
             throw new Exception("Oops. Someting went wrong during login. Please try again", 401);
         }
         $session = Session::singleton();
         $db = Database::singleton();
         if (!isset($identityProviders[$idp])) {
             throw new Exception("Unknown identity provider", 500);
         }
         $client_id = $identityProviders[$idp]['clientId'];
         $client_secret = $identityProviders[$idp]['clientSecret'];
         $redirect_uri = $identityProviders[$idp]['redirectUrl'];
         $token_url = $identityProviders[$idp]['tokenUrl'];
         $api_url = $identityProviders[$idp]['apiUrl'];
         $emailField = $identityProviders[$idp]['emailField'];
         // instantiate authController
         $authController = new OAuthLoginController($client_id, $client_secret, $redirect_uri, $token_url);
         // request token
         if ($authController->requestToken($code)) {
             // request data
             if ($authController->requestData($api_url)) {
                 // Verify email/role here
                 $email = $authController->getData()->{$emailField};
                 // Get user with $email
                 // Set sessionUser
                 $interface = new InterfaceObject('EmailUser');
                 $atom = new Atom($email, 'Email');
                 $users = array_keys((array) $atom->getContent($interface, true));
                 // create new user
                 if (empty($users)) {
                     $newUser = Concept::createNewAtom('User');
                     $db->addAtomToConcept($newUser, 'User');
                     $db->editUpdate('userEmail', false, $newUser, 'User', $email, 'Email');
                     // add to Organization
                     $domain = explode('@', $email)[1];
                     $interface = new InterfaceObject('DomainOrgs');
                     $atom = new Atom($domain, 'Domain');
                     $orgs = array_keys((array) $atom->getContent($interface, true));
                     foreach ($orgs as $org) {
                         $db->editUpdate('userOrganization', false, $newUser, 'User', $org, 'Organization');
                     }
                     $users[] = $newUser;
                 }
                 if (count($users) > 1) {
                     throw new Exception("Multiple users registered with email {$email}", 401);
                 }
                 foreach ($users as $userId) {
                     // Set sessionUser
                     $db->editUpdate('sessionUser', false, session_id(), 'SESSION', $userId, 'User');
                     // Timestamps
                     $db->editUpdate('userLastLoginTimeStamp', false, $userId, 'User', date(DATE_ISO8601), 'DateTime');
                     $db->editUpdate('userLoginTimeStamp', false, $userId, 'User', date(DATE_ISO8601), 'DateTime');
                 }
                 $db->closeTransaction('Login successfull', false, true, false);
             }
         }
         header('Location: ' . Config::get('serverURL'));
         exit;
     } catch (Exception $e) {
         throw new RestException($e->getCode(), $e->getMessage());
     }
 }