1
0
Fork 0
mirror of synced 2024-06-29 03:30:34 +12:00

Merge pull request #128 from christyjacob4/master

Added endpoint for updating user preferences from the users service
This commit is contained in:
Eldad A. Fux 2019-10-05 13:44:08 +03:00 committed by GitHub
commit c5f26121a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 177 additions and 0 deletions

View file

@ -56,4 +56,9 @@ return [
'icon' => 'icon-dropbox',
'enabled' => true,
],
'apple' => [
'developers' => 'https://www.dropbox.com/developers/documentation',
'icon' => 'icon-apple',
'enabled' => false,
],
];

View file

@ -381,6 +381,38 @@ $utopia->patch('/v1/users/:userId/status')
}
);
$utopia->patch('/v1/users/:userId/prefs')
->desc('Update Account Prefs')
->label('scope', 'users.write')
->label('sdk.namespace', 'users')
->label('sdk.method', 'updateUserPrefs')
->param('userId', '', function () {
return new UID();
}, 'User unique ID.')
->param('prefs', '', function () {
return new \Utopia\Validator\Mock();
}, 'Prefs key-value JSON object string.')
->label('sdk.description', 'Update user preferences by its unique ID. You can pass only the specific settings you wish to update.')
->action(
function ($userId, $prefs) use ($response, $projectDB) {
$user = $projectDB->getDocument($userId);
if (empty($user->getUid()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
throw new Exception('User not found', 404);
}
$user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [
'prefs' => json_encode(array_merge(json_decode($user->getAttribute('prefs', '{}'), true), $prefs)),
]));
if (false === $user) {
throw new Exception('Failed saving user to DB', 500);
}
$response->json(array('result' => 'success'));
}
);
$utopia->delete('/v1/users/:userId/sessions/:session')
->desc('Delete User Session')
->label('scope', 'users.write')

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

133
src/Auth/OAuth/Apple.php Normal file
View file

@ -0,0 +1,133 @@
<?php
namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple
class Apple extends OAuth
{
/**
* @var array
*/
protected $user = [];
/**
* @return string
*/
public function getName(): string
{
return 'apple';
}
/**
* @return string
*/
public function getLoginURL(): string
{
return 'https://appleid.apple.com/auth/authorize?'.
'client_id='.urlencode($this->appID).
'&redirect_uri='.urlencode($this->callback).
'&state='.urlencode(json_encode($this->state)).
'&response_type=code'.
'&response_mode=form_post'.
'&scope=name+email';
}
/**
* @param string $code
*
* @return string
*/
public function getAccessToken(string $code): string
{
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$accessToken = $this->request(
'POST',
'https://appleid.apple.com/auth/token',
$headers,
'code='.urlencode($code).
'&client_id='.urlencode($this->appID).
'&client_secret='.urlencode($this->appSecret).
'&redirect_uri='.urlencode($this->callback).
'&grant_type=authorization_code'
);
var_dump($accessToken);
exit();
$accessToken = json_decode($accessToken, true);
if (isset($accessToken['access_token'])) {
return $accessToken['access_token'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['account_id'])) {
return $user['account_id'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['email'])) {
return $user['email'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['name'])) {
return $user['name']['display_name'];
}
return '';
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
if (empty($this->user)) {
$headers[] = 'Authorization: Bearer '. urlencode($accessToken);
$user = $this->request('POST', 'https://api.dropboxapi.com/2/users/get_current_account', $headers);
$this->user = json_decode($user, true);
}
return $this->user;
}
}

View file

@ -4,6 +4,9 @@ namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://docs.gitlab.com/ee/api/oauth2.html
class Gitlab extends OAuth
{
/**

View file

@ -4,6 +4,10 @@ namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://developers.google.com/oauthplayground/
// https://developers.google.com/identity/protocols/OAuth2
// https://developers.google.com/identity/protocols/OAuth2WebServer
class Google extends OAuth
{
/**