Last active
January 22, 2019 07:27
-
-
Save f-f/79a7a168e50bbd53dc20c11143f335bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PersonaUser { | |
private function isEntitled($uuid = null, $token = null) { | |
$allowed_products = array("LS DIGI"); | |
$entitled = false; | |
$personaUrl = "https://persona.api.ksfmedia.fi/v1" | |
$personaConfig = new \OpenAPI\Client\Configuration(); | |
$personaConfig->setHost($personaUrl); | |
$user = null; | |
$is_entitled_due_to_persona_err = false; | |
$persona = new \OpenAPI\Client\Api\UsersApi(null, $personaConfig, null); | |
// Here you authenticate the user - you always have to reauthenticate them | |
// backend side otherwise you cannot trust the token | |
try { | |
try { | |
// Here you might want to have your uuid and token saved in a cookie or something | |
if (isset($uuid) && isset($token)) { | |
$user = $persona->usersUuidGet($uuid, "OAuth " . $token); | |
} | |
} catch (\OpenAPI\Client\ApiException $e) { | |
switch ($e->getCode()) { | |
case 500: | |
case 502: | |
case 503: | |
$is_entitled_due_to_persona_err = true; | |
break; | |
default: | |
throw new \Exception($e->getMessage()); | |
} | |
} | |
} catch (\Exception $e) { | |
$this->logger->debug('Calling Persona failed'); | |
} | |
// Entitlement checking starts here | |
if ($is_entitled_due_to_persona_err) { | |
return true; | |
} | |
if ($user == null) { | |
return $entitled; | |
} | |
$this->logger->debug("PersonaUser->isEntitled() Checking cusno:" . $this->user['cusno']); | |
foreach ($user->getSubs() as $sub) { | |
if ($sub->getDates()->getEnd() < new \DateTime()) { | |
continue; | |
} | |
foreach ($sub->getPackage()->getProducts() as $product) { | |
if (in_array($product->getName(), $allowed_products)) { | |
$this->logger->debug("Found entitlement: " . $product->getName()); | |
$entitled = true; | |
} | |
} | |
} | |
$this->logger->debug("cusno: " . $user['cusno'] . " entitled: " . $entitled); | |
return $entitled; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment