Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User pagination and fields, docblocks, formatting, test improvements #261

Merged
merged 5 commits into from
Jun 13, 2018

Conversation

joshcanhelp
Copy link
Contributor

  • Add fields and pagination parameters to Users::getAll() method
  • Docblocks and method parameter comments
  • Re-write User tests to match Connections and Clients

@joshcanhelp joshcanhelp added this to the v5-Next milestone Jun 8, 2018
@joshcanhelp joshcanhelp mentioned this pull request Jun 11, 2018
Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I saw this on other entities, but it's missing on this one. Maybe I'm confusing the repo. But I think it's missing the "scopes" required in the token for each call on the doc block.

* @param string $user_id - User ID to update.
* @param array $data - User data to update:
* - Only certain fields can be updated; see the @link below for allowed fields.
* - "user_metadata" and "app_metadata" fields are merged, not replaced.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is true for this fields but also for the rest of the fields under User. I do like the clarification though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the API docs:

The properties of the new object will replace the old ones ... The metadata fields are an exception to this rule

Am I not capturing that correctly or are the docs wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Users behave differently, ignore my first comment.

* Create a new User.
*
* @param array $data - User create data:
* - "connection" name field is required and limited to sms, email, & DB connections.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of "DB" I'd use "sms, email and auth0 (Username and Password) strategies". I don't think you are allowed to create "ad" db users with this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little hesitant to mix strategies in here. This field is looking for a connection name, not a strategy.

Should we clarify what kind of user can be created here?

Copy link
Contributor

@lbalmaceda lbalmaceda Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is looking for a connection name, not a strategy.

true, but those connections must belong to "one of this strategies: sms, email, auth0"

* @param array $data - User create data:
* - "connection" name field is required and limited to sms, email, & DB connections.
* - "phone_number" field is required by sms connections.
* - "email" field is required by email and DB connections.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on the connection settings, "username" could be required for sign-ups as well.


// Passwords are required for DB connections.
if ('email' !== $data['connection'] && empty($data['password'])) {
throw new \Exception('Missing required "password" field for DB connection.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of 'db' use the name of the connection you are being passed.

*
* @link https://auth0.com/docs/users/search/v3#migrate-from-search-engine-v2-to-v3
* @link https://auth0.com/docs/api/management/v2#!/Users/get_users
* @link https://auth0.com/docs/users/search/best-practices
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see many links to sections under /docs/users/search. Why not just point to that page? Anchors to sections may break if a char on the title is changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First link is an anchor, the rest are direct. I wouldn't imagine that the migration guide link will change as it's in a number of places.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just point to https://auth0.com/docs/users/search/v3 since that's the main page for the search API. Also, in that article there's a link to v2 syntax. So just keep:

*
* @return bool
*/
protected function errorHasString(\Exception $e, $str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why \ precedes Exception always ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\ indicates global namespce

*
* @var integer
*/
protected $rand;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems common across entities tests. What about moving this up to the BasicCrudTest class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch ... that was kind of silly :)

protected function getAllEntities()
{
$fields = array_keys($this->getCreateBody());
$fields[] = $this->id_name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this line doing? is like no index is passed to the array? 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$fields is an array of fields to get, created initially as the keys from the createBody array. This adds the id field to that array so we get the id as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but since "arrays in php are not arrays but dictionaries", am I right to assume this will end up adding a key named user_id (specified here https://github.com/auth0/auth0-PHP/pull/261/files/b018f664c75fbfaca8c8febb9148eacb2002d58f#diff-f6b74f1841de4422909e8c5982be5c87R19) with a null value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is just a plain array, numeric keys:

[ 'connection', 'email', 'password', 'picture', 'email_verified', 'user_metadata', 'user_id' ]

$this->domain = $env['DOMAIN'];
// Get the second page of Users with 1 per page (second result).
$paged_results = $this->api->getAll([
'sort' => 'created_at:1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the rest of the entities do the same sorting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I added this because we're likely to get back a lot of results and this will ensure the pagination check works regardless of the number. Other endpoints don't appear to support sorting.

try {
$this->api->create([]);
} catch (\Exception $e) {
$caught_connection_error = $this->errorHasString($e, 'connection');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit vague. I don't remember the full error message but what about searching for something like "missing param 'connection'" at least? Same for the checks below

@joshcanhelp
Copy link
Contributor Author

@lbalmaceda - Also added scopes here, good suggestion ... I'll make a note to add that to other docblocks

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly docs changes

{
/**
* Get a single User by ID.
* Requires scopes: read:users, read:user_idp_tokens.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion -> Enclose values in quotes. (applies for all docblocks)

IMHO required -> explain when the read:user_idp_tokens scope would be required. It's not that to call this endpoint you must provide it, only if you want to obtain the access_tokens for each logged in identity provider.

/**
* Update a User.
* Requires scope: update:users.
* May require scope: update:users_app_metadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahá!! again, when would this scope be required? Don't say it's implicit on the scope value. You can also rephrase it in a similar way to this:

Requires scope 'update:users'. If the values being changed include the 'user_metadata' or 'app_metadata' fields, additionally the scope must include 'update:users_app_metadata'.

* - "phone_number" field is required by sms connections.
* - "email" field is required by email and DB connections.
* - "password" field is required by DB connections.
* - Depending on the connection user, other fields may be required.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you meant user->used ??
Also, I don't think we enforce other fields apart from username. (We don't provide a way to customize this either AFAIK, maybe good to look at "custom db sign up scripts"). If it's that one alone, why not just name it?

$client = $this->apiClient->get()
->users();
// Fields to include/exclude.
if (!isset($params['fields']) && null !== $fields) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no debugging is needed as long as it's well documented. Being one case or the other. Both are corrects. I like more the one I suggested in my previous comment since the previous signature didn't have anything but the params dictionary, so anything added later (talking about order) would replace them, but I accept yours as well 👍 Choose whatever that sticks to the other entities' methods behavior.

if (is_array($params['fields'])) {
$params['fields'] = implode(',', $params['fields']);
}
if (null !== $include_fields) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you're not checking that include_fields is first set on params, so you're overriding it with the explicit parameter.

* Requires scope: update:users.
*
* @param string $user_id - User ID of the primary identity
* @param array $data - Secondary identity to link; either link_with JWT or provider, connection_id, and user_id.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding something like "see ways of calling this endpoint in the @link below", like you did for users search v3 above. Or at least add a bulleted list below the data param to differentiate both payloads. (I still find it confusing)

i.e.

- Sending the JWT as the link_with parameter
- Sending the provider, connection_id and user_id values.

* Should the results returned by getAll be searched for the created user?
*
* @var bool
*/
protected $findCreatedItem = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, doesn't this apply for all the entities with a CREATE method? Should it be generalized like the rand in the super class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's an option of sorts

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🎓

@joshcanhelp joshcanhelp merged commit 11b9e3f into master Jun 13, 2018
@joshcanhelp joshcanhelp deleted the add-users-pagination-tests branch June 13, 2018 04:34
@github-actions
Copy link
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants