adds controller for creating a user target
This commit is contained in:
parent
455176d077
commit
07b7a9db0f
7 changed files with 105 additions and 8 deletions
|
@ -1704,16 +1704,16 @@ $commonCollections = [
|
||||||
'format' => '',
|
'format' => '',
|
||||||
'size' => Database::LENGTH_KEY,
|
'size' => Database::LENGTH_KEY,
|
||||||
'signed' => true,
|
'signed' => true,
|
||||||
'required' => true,
|
'required' => false,
|
||||||
'default' => null,
|
'default' => null,
|
||||||
'array' => false,
|
'array' => false,
|
||||||
'filters' => [],
|
'filters' => ['subQueryProviderType'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'$id' => ID::custom('identifier'),
|
'$id' => ID::custom('identifier'),
|
||||||
'type' => Database::VAR_STRING,
|
'type' => Database::VAR_STRING,
|
||||||
'format' => '',
|
'format' => '',
|
||||||
'size' => 2048,
|
'size' => Database::LENGTH_KEY,
|
||||||
'signed' => true,
|
'signed' => true,
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'default' => null,
|
'default' => null,
|
||||||
|
|
|
@ -232,7 +232,12 @@ return [
|
||||||
],
|
],
|
||||||
Exception::USER_TARGET_NOT_FOUND => [
|
Exception::USER_TARGET_NOT_FOUND => [
|
||||||
'name' => Exception::USER_TARGET_NOT_FOUND,
|
'name' => Exception::USER_TARGET_NOT_FOUND,
|
||||||
'description' => 'The current user target could not be found.',
|
'description' => 'The target could not be found.',
|
||||||
|
'code' => 404,
|
||||||
|
],
|
||||||
|
Exception::USER_TARGET_ALREADY_EXISTS => [
|
||||||
|
'name' => Exception::USER_TARGET_ALREADY_EXISTS,
|
||||||
|
'description' => 'A target with the same ID already exists.',
|
||||||
'code' => 404,
|
'code' => 404,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
@ -664,4 +669,11 @@ return [
|
||||||
'description' => 'Too many queries.',
|
'description' => 'Too many queries.',
|
||||||
'code' => 400,
|
'code' => 400,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
/** Provider Errors */
|
||||||
|
Exception::PROVIDER_NOT_FOUND => [
|
||||||
|
'name' => Exception::PROVIDER_NOT_FOUND,
|
||||||
|
'description' => 'Provider with the request ID could not be found.',
|
||||||
|
'code' => 400,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
|
@ -20,6 +20,8 @@ $member = [
|
||||||
'avatars.read',
|
'avatars.read',
|
||||||
'execution.read',
|
'execution.read',
|
||||||
'execution.write',
|
'execution.write',
|
||||||
|
'targets.read',
|
||||||
|
'targets.write',
|
||||||
];
|
];
|
||||||
|
|
||||||
$admins = [
|
$admins = [
|
||||||
|
@ -51,6 +53,8 @@ $admins = [
|
||||||
'functions.write',
|
'functions.write',
|
||||||
'execution.read',
|
'execution.read',
|
||||||
'execution.write',
|
'execution.write',
|
||||||
|
'targets.read',
|
||||||
|
'targets.write',
|
||||||
];
|
];
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -1672,7 +1672,7 @@ App::get('/v1/account/targets')
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'targets' => $targets,
|
'targets' => $targets,
|
||||||
'total' => count($targets),
|
'total' => \count($targets),
|
||||||
]), Response::MODEL_TARGET_LIST);
|
]), Response::MODEL_TARGET_LIST);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -363,6 +363,66 @@ App::post('/v1/users/scrypt-modified')
|
||||||
->dynamic($user, Response::MODEL_USER);
|
->dynamic($user, Response::MODEL_USER);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
App::post('/v1/users/:userId/targets')
|
||||||
|
->desc('Create User Target')
|
||||||
|
->groups(['api', 'users'])
|
||||||
|
->label('event', 'users.[userId].targets.[targetId].create')
|
||||||
|
->label('scope', 'targets.write')
|
||||||
|
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
|
||||||
|
->label('sdk.namespace', 'users')
|
||||||
|
->label('sdk.method', 'createTarget')
|
||||||
|
->label('sdk.description', '/docs/references/users/create-target.md')
|
||||||
|
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
|
||||||
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||||
|
->label('sdk.response.model', Response::MODEL_TARGET)
|
||||||
|
->param('targetId', '', new UID(), 'Target ID.', false)
|
||||||
|
->param('userId', '', new UID(), 'ID of the user.', false)
|
||||||
|
->param('providerId', '', new UID(), 'ID of the provider.', false)
|
||||||
|
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)', false)
|
||||||
|
->inject('response')
|
||||||
|
->inject('project')
|
||||||
|
->inject('dbForProject')
|
||||||
|
->inject('events')
|
||||||
|
->action(function (string $targetId, string $userId, string $providerId, string $identifier, Response $response, Document $project, Database $dbForProject, Event $events) {
|
||||||
|
$provider = $dbForProject->getDocument('providers', $providerId);
|
||||||
|
|
||||||
|
if ($provider->isEmpty()) {
|
||||||
|
throw new Exception(Exception::PROVIDER_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $dbForProject->getDocument('users', $userId);
|
||||||
|
|
||||||
|
if($user->isEmpty()) {
|
||||||
|
throw new Exception(Exception::USER_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
$target = $dbForProject->getDocument('targets', $targetId);
|
||||||
|
|
||||||
|
if(!$target->isEmpty()) {
|
||||||
|
throw new Exception(Exception::USER_TARGET_ALREADY_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
$target = $dbForProject->createDocument('targets', new Document([
|
||||||
|
'$id' => $targetId,
|
||||||
|
// TO DO: what permissions should be given when created a target.
|
||||||
|
'$permissions' => [
|
||||||
|
Permission::read(Role::any())
|
||||||
|
],
|
||||||
|
'providerId' => $providerId,
|
||||||
|
'providerInternalId' => $provider->getInternalId(),
|
||||||
|
'providerType' => null,
|
||||||
|
'userId' => $userId,
|
||||||
|
'userInternalId' => $user->getInternalId(),
|
||||||
|
'identifier' => $identifier,
|
||||||
|
]));
|
||||||
|
$events
|
||||||
|
->setParam('userId', $userId)
|
||||||
|
->setParam('targetId', $targetId);
|
||||||
|
$response
|
||||||
|
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||||
|
->dynamic($target, Response::MODEL_TARGET);
|
||||||
|
});
|
||||||
|
|
||||||
App::get('/v1/users')
|
App::get('/v1/users')
|
||||||
->desc('List Users')
|
->desc('List Users')
|
||||||
->groups(['api', 'users'])
|
->groups(['api', 'users'])
|
||||||
|
@ -677,10 +737,9 @@ App::get('/v1/users/:userId/targets')
|
||||||
}
|
}
|
||||||
|
|
||||||
$targets = $user->getAttribute('targets', []);
|
$targets = $user->getAttribute('targets', []);
|
||||||
var_dump($user);
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'targets' => $targets,
|
'targets' => $targets,
|
||||||
'total' => count($targets),
|
'total' => \count($targets),
|
||||||
]), Response::MODEL_TARGET_LIST);
|
]), Response::MODEL_TARGET_LIST);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
19
app/init.php
19
app/init.php
|
@ -535,6 +535,25 @@ Database::addFilter(
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Database::addFilter(
|
||||||
|
'subQueryProviderType',
|
||||||
|
function (mixed $value) {
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
function (mixed $value, Document $document, Database $database) {
|
||||||
|
$provider = Authorization::skip(fn() => $database
|
||||||
|
->findOne('providers', [
|
||||||
|
Query::equal('$id', [$document->getAttribute('providerId')]),
|
||||||
|
Query::select(['type']),
|
||||||
|
Query::limit(APP_LIMIT_SUBQUERY),
|
||||||
|
]));
|
||||||
|
if($provider)
|
||||||
|
return $provider->getAttribute('type');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DB Formats
|
* DB Formats
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -82,7 +82,7 @@ class Exception extends \Exception
|
||||||
public const USER_OAUTH2_UNAUTHORIZED = 'user_oauth2_unauthorized';
|
public const USER_OAUTH2_UNAUTHORIZED = 'user_oauth2_unauthorized';
|
||||||
public const USER_OAUTH2_PROVIDER_ERROR = 'user_oauth2_provider_error';
|
public const USER_OAUTH2_PROVIDER_ERROR = 'user_oauth2_provider_error';
|
||||||
public const USER_TARGET_NOT_FOUND = 'user_target_not_found';
|
public const USER_TARGET_NOT_FOUND = 'user_target_not_found';
|
||||||
|
public const USER_TARGET_ALREADY_EXISTS = 'user_target_already_exists';
|
||||||
/** Teams */
|
/** Teams */
|
||||||
public const TEAM_NOT_FOUND = 'team_not_found';
|
public const TEAM_NOT_FOUND = 'team_not_found';
|
||||||
public const TEAM_INVITE_ALREADY_EXISTS = 'team_invite_already_exists';
|
public const TEAM_INVITE_ALREADY_EXISTS = 'team_invite_already_exists';
|
||||||
|
@ -205,6 +205,9 @@ class Exception extends \Exception
|
||||||
public const GRAPHQL_NO_QUERY = 'graphql_no_query';
|
public const GRAPHQL_NO_QUERY = 'graphql_no_query';
|
||||||
public const GRAPHQL_TOO_MANY_QUERIES = 'graphql_too_many_queries';
|
public const GRAPHQL_TOO_MANY_QUERIES = 'graphql_too_many_queries';
|
||||||
|
|
||||||
|
/** Provider */
|
||||||
|
public const PROVIDER_NOT_FOUND = 'provider_not_found';
|
||||||
|
|
||||||
protected $type = '';
|
protected $type = '';
|
||||||
protected $errors = [];
|
protected $errors = [];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue