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

GET, POST, PUT Member permissions #74

Open
nimeso opened this issue Nov 23, 2016 · 5 comments
Open

GET, POST, PUT Member permissions #74

nimeso opened this issue Nov 23, 2016 · 5 comments

Comments

@nimeso
Copy link

nimeso commented Nov 23, 2016

I have created two api Members and I'm using token authentication. Each Member has a unique API Key

  • super admin API admin users - should be able to GET, POST, PUT etc
  • content API users - should only be able to GET but NOT POST, ETC

I've changed the permissions for each Member under the permissions tab for each user in the CMS so that the 'content API user' only has 'Access records through the RESTful API' checked BUT I can still POST, PUT records using the users API key?

I hope thats not to confusing

api_user

@nimeso
Copy link
Author

nimeso commented Nov 23, 2016

I have asked this question before and never worked out how to do it. If you could possibly send me some example code so I can get my head around it :) I'm more than happy to pay for your time as this project need to be finished in the next 2 days! Arrrgh! contact me on [email protected] if you can help solve this issue for me once and for all. I just need to see how permissions would work on a simple DataObject with two different Members. Thanks!

@nimeso
Copy link
Author

nimeso commented Nov 23, 2016

Hmmmm... looks promising

RESTfulAPI_GroupExtension
my members are in different groups already with correct permissions set.

How does this work?

@colymba
Copy link
Owner

colymba commented Nov 24, 2016

Hey @nimeso I'll put the answer here for all to see. Happy to help for free :)

Basically the module comes with a few tools/components to make access control easier, but you still have to write a little bit of code yourself for finish the implementation.

First some config to enable access control so it checks Member's permissions, by changing access_control_policy

RESTfulAPI:
  access_control_policy: 'ACL_CHECK_CONFIG_AND_MODEL'

This tell the API to both check the requested model api_access config and then check permission on the model through canView/Edit/Create/Delete.

Then we need some Permissions and Groups to add Members to and give them different access level to check against.

The RESTfulAPI_GroupExtension comes with a set of basic API Permissions and Groups that can be used. We'll just add it in our config:

Group:
  extensions:
    - RESTfulAPI_GroupExtension

Now we have a set of Groups named restfulapi-readers, restfulapi-editors and restfulapi-administrators that have different sets of Permissions (view only, view+edit+create and all).

Now in the CMS you can add your Members to the groups you want. In your case 'super admin API admin users' would be in the restfulapi-administrators group and 'content API users' would be in the restfulapi-readers group.

Now that it is all setup we can enforce those permissions on our DataObjects, in their canView/Edit/Create/Delete methods. So you would have to add this to all the DataObjects accessible via the API:

  public function canView($member = null)
  {
    return Permission::check('RESTfulAPI_VIEW', 'any', $member);
  }

  public function canEdit($member = null)
  {
    return Permission::check('RESTfulAPI_EDIT', 'any', $member);
  }

  public function canCreate($member = null)
  {
    return Permission::check('RESTfulAPI_CREATE', 'any', $member);
  }

  public function canDelete($member = null)
  {
    return Permission::check('RESTfulAPI_DELETE', 'any', $member);
  }

Adding this code to all the DataObjects is a bit laborious, that could probably be added to a DataExtension in the future...

All this should get it working. Try it out and let me know.

@nimeso
Copy link
Author

nimeso commented Nov 24, 2016 via email

@colymba
Copy link
Owner

colymba commented Nov 24, 2016

Glad it all worked out!

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

No branches or pull requests

2 participants