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

Add /api/admin authorization #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

puckey
Copy link
Contributor

@puckey puckey commented Jul 27, 2020

I noticed that the /api/admin/dummies endpoints were not protected with the same authentication as the admin panel.

Perhaps there is a better way to do this, but adding authorize = ctx => ctx.isAuthenticated() to the /api/admin/dummies controller does the trick.

@lehni
Copy link
Contributor

lehni commented Jul 30, 2020

The dito way is something like this:

authorize = 'admin', with 'admin' being a role that the user is matched against. The user can define a method $hasRole() for this purpose. The default checks an optional roles array property. I think we should use this in the example app.

@lehni
Copy link
Contributor

lehni commented Jul 30, 2020

There is some documentation about this in the comments of processAuthorize():

          // Support 3 scenarios:
          // - '$self': The requested member is checked against `ctx.state.user`
          //   and the action is only authorized if it matches the member.
          // - '$owner': The member is asked if it is owned by `ctx.state.user`
          //   through the optional `Model.$hasOwner()` method.
          // - any string:  `ctx.state.user` is checked for this role through
          //   the overridable `UserModel.hasRole()` method.

@lehni
Copy link
Contributor

lehni commented Jul 30, 2020

You can also check against multiple roles by providing an array: authorize = ['admin', 'superuser'] And you can provide a function that returns a string or array to check roles against. Lots of possibilities :)

@lehni
Copy link
Contributor

lehni commented Jul 30, 2020

authorize can be set on a per controller and per action level

@puckey
Copy link
Contributor Author

puckey commented Jul 31, 2020

Okay, so this entails adding a roles property to the User model's properties. If this is the Dito way, would it be an idea to have a roles property in the User model by default with options for admin and editor for example?

puckey added 2 commits July 31, 2020 17:10
# Conflicts:
#	src/server/controllers/api/admin/dummies.js
@puckey
Copy link
Contributor Author

puckey commented Jul 31, 2020

I added user roles to this PR here: b03ebdb

@puckey
Copy link
Contributor Author

puckey commented Jul 31, 2020

I guess a next step would be to allow editing / creating users when you have the 'admin' role and editing yourself when you have the 'editor' role

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants