gpt4 book ai didi

php - 如何使用 Rest API 在 Processmaker 3.0 中创建注销功能?

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:31 26 4
gpt4 key购买 nike

我正在 Process-maker 3.0 中开发一个 REST API。其中用户可以使用密码oauth2.0授权登录。

我们获得访问 token 并且 Oauthcredential.json 得到自动更新。当用户使用凭据(client_id、client_secret、用户名和密码)登录时,cookie 集。它按照链接中的建议指向 REST 端点: http://wiki.processmaker.com/3.0/Calling_REST_Endpoints

当 cookie 未设置或被清除时,它应该重定向到登录页面,或者当用户单击注销按钮时,它将重定向到登录页面。

登录页面代码

'<html><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="check_login.php" method="post">
Client ID<br>
<input type="text" name="client_id" value="" width=32 /><br>
Client Secret<br>
<input type="text" name="client_secret" value="" width=32 /><br>
Username<br>
<input type="text" name="username" value="" width=20 /><br>
Password<br>
<input type="text" name="password" value="" width=20 /><br>
<input type="submit" value="Login"/>
</form>
</body>
</html>

登录成功后进入checklogin.php页面

<?php
$clientId=isset($_POST['client_id']);
$clientSecret=isset($_POST['clientSecret']);
$username=isset($_POST['username']);
$password=isset($_POST['password']);


//change the server address and workspace to match your system:
$pmServer = "http://127.0.0.1/api/1.0/workflow";
$pmWorkspace = 'workflow';

function pmRestLogin($clientId, $clientSecret, $username, $password) {
global $pmServer, $pmWorkspace;
$postParams = array(
'grant_type' => 'password',
'scope' => '*', //set to 'view_process' if not changing the process
'client_id' => $clientId,
'client_secret' => $clientSecret,
'username' => $username,
'password' => $password
);
echo "after function";
$ch = curl_init("$pmServer/oauth2/token");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$oToken = json_decode(curl_exec($ch));
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpStatus != 200) {
print "Error in HTTP status code: $httpStatus\n";
return null;
}
else if (isset($oToken->error)) {
print "Error logging into $pmServer:\n" .
"Error: {$oToken->error}\n" .
"Description: {$oToken->error_description}\n";
}
else {
//At this point $oToken->access_token can be used to call REST endpoints.

//If planning to use the access_token later, either save the access_token
//and refresh_token as cookies or save them to a file in a secure location.

//If saving them as cookies:
setcookie("access_token", $oToken->access_token, time() + 60*5);
setcookie("refresh_token", $oToken->refresh_token); //refresh token doesn't expire
setcookie("client_id", $clientId);
setcookie("client_secret", $clientSecret);


echo "saving cred in file";
//If saving to a file:
file_put_contents("oauthCredentials.json", json_encode($oToken));
//include the path in the filename if not located in the same directory:

}

return $oToken;
}


$oToken = pmRestLogin($_POST['client_id'], $_POST['client_secret'],$_POST['username'], $_POST['password']);

if (isset($oToken) and isset($oToken->access_token)) {
//can now call REST endpoints using $oToken->access_token
// $oRet = pmRestRequest("GET", "/api/1.0/workflow/users", null, $oToken- >access_token);
header("location: cases.php");

}
?>

登录成功后进入cases.php`

            <?php
$pmServer = "http://127.0.0.1"; //set to your ProcessMaker address

$accessToken = isset($_COOKIE['access_token']) ? $_COOKIE['access_token'] : getAccessToken();

/*check cookie expired or not*/
if (empty($accessToken) and isset($_COOKIE['access_token']))
$accessToken = $_COOKIE['access_token'];

if (empty($accessToken)) { //if the access token has expired
//To check if the PM login session has expired: !isset($_COOKIE['PHPSESSID'])
header("Location: formLogin.php"); //change to match your login method
die();
}
/***************************/


$ch = curl_init($pmServer . "/api/1.0/workflow/users");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $accessToken));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$aUsers = json_decode(curl_exec($ch));
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($statusCode != 200) {
/*if (isset ($aUsers) and isset($aUsers->error))
print "Error code: {$aUsers->error->code}\nMessage: {$aUsers->error->message}\n";
else
print "Error: HTTP status code: $statusCode\n";*/
header("Location: formLogin.php"); //change to match your login method
die();

}
else {
foreach ($aUsers as $oUser) {
if ($oUser->usr_status == "ACTIVE") {
print "{$oUser->usr_firstname} {$oUser->usr_lastname} ({$oUser->usr_username})\n";
}
}
}
?>



<body>
<div data-role="page">
<div data-role="header" data-position="fixed">
<h1>My Cases</h1>
</div>

<div role="main" class="ui-content">
<ul data-role="listview" data-inset="false" data-divider-theme="a">
<li data-role="list-divider">Home</li>
<li><a href="todo-list.html">Inbox</a></li>
<li><a href="jlogin.html">Logout</a></li>
</ul>
</div>

<div data-role="footer" data-position="fixed">
</div>
</div>


</body>


</html>`

在 cases.php 中,当未设置 session ID 时,它应该重定向到 formLogin.php,但此功能无法正常工作。

提前致谢。

最佳答案

我建议不要使用 http 代码来检查是否有人登录。原因是:如果有错误代码,例如 404 或其他无法正常工作,但用户不希望注销,您应该能够在您的应用程序 UI 中处理它。

相反,我建议使用 session 变量来存储访问 token ,然后当用户单击注销按钮时,只需销毁该变量,然后将它们重定向到登录页面。

这是我使用 ProcessMaker rest api 构建的一个应用程序示例,它允许您使用授权代码授权类型通过 oauth 2 登录并处理注销。我的应用程序和你的应用程序之间的唯一区别是,我的应用程序是用 angularjs 编写的 SPA,而你的应用程序是用 php 编写的。虽然概念是相同的。

https://github.com/ethnp/pmangular

关于php - 如何使用 Rest API 在 Processmaker 3.0 中创建注销功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37109934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com