diff --git a/workflow/engine/controllers/designer.php b/workflow/engine/controllers/designer.php index 96b6da80c..ca92950a5 100644 --- a/workflow/engine/controllers/designer.php +++ b/workflow/engine/controllers/designer.php @@ -9,6 +9,8 @@ class Designer extends Controller { + protected $clientId = 'x-pm-local-client'; + public function __construct () { @@ -22,11 +24,50 @@ class Designer extends Controller public function index($httpData) { $proUid = isset($httpData->pro_uid) ? $httpData->pro_uid : ''; + $client = $this->getClientCredentials(); + $authCode = $this->getAuthorizationCode($client); - $this->setVar('pro_uid', $proUid); + $this->setVar('prj_uid', $proUid); + $this->setVar('client_id', $client['CLIENT_ID']); + $this->setVar('secret', $client['CLIENT_SECRET']); + $this->setVar('authorization_code', $authCode); $this->setView('designer/index'); - $this->render(); } + + protected function getClientCredentials() + { + $oauthQuery = new Services\Api\OAuth2\PmPdo($this->getDsn()); + return $oauthQuery->getClientDetails($this->clientId); + } + + protected function getAuthorizationCode($client) + { + \Services\Api\OAuth2\Server::setDatabaseSource($this->getDsn()); + \Services\Api\OAuth2\Server::setPmClientId($client['CLIENT_ID']); + + $oauthServer = new \Services\Api\OAuth2\Server(); + $userId = $_SESSION['USER_LOGGED']; + $authorize = true; + $_GET = array_merge($_GET, array( + 'response_type' => 'code', + 'client_id' => $client['CLIENT_ID'], + 'scope' => implode(' ', $oauthServer->getScope()) + )); + + $response = $oauthServer->postAuthorize($authorize, $userId, true); + $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40); + + return $code; + } + + private function getDsn() + { + list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, ''); + $port = empty($port) ? '' : ";port=$port"; + $dsn = DB_ADAPTER.':host='.$host.';dbname='.DB_NAME.$port; + + return array('dsn' => $dsn, 'username' => DB_USER, 'password' => DB_PASS); + } } diff --git a/workflow/engine/src/Services/Api/OAuth2/Server.php b/workflow/engine/src/Services/Api/OAuth2/Server.php index 8a14cd903..f9081c693 100644 --- a/workflow/engine/src/Services/Api/OAuth2/Server.php +++ b/workflow/engine/src/Services/Api/OAuth2/Server.php @@ -63,11 +63,17 @@ class Server implements iAuthenticate $this->server->setScopeUtil($scope); } - public static function setDatabaseSource($user, $password, $dsn) + public static function setDatabaseSource($user, $password = '', $dsn = '') { - self::$dbUser = $user; - self::$dbPassword = $password; - self::$dsn = $dsn; + if (is_array($user)) { + self::$dbUser = $user['username']; + self::$dbPassword = $user['password']; + self::$dsn = $user['dsn']; + } else { + self::$dbUser = $user; + self::$dbPassword = $password; + self::$dsn = $dsn; + } } /** @@ -224,4 +230,9 @@ class Server implements iAuthenticate { return self::$userId; } + + public function getScope() + { + return array_keys($this->scope); + } } diff --git a/workflow/engine/templates/designer/index.html b/workflow/engine/templates/designer/index.html index 833bddf1a..eed7c190a 100644 --- a/workflow/engine/templates/designer/index.html +++ b/workflow/engine/templates/designer/index.html @@ -5,6 +5,12 @@ + @@ -14,6 +20,4 @@ - - -