0

I use services with drupal 7, here the services resources function :

function custom_api_services_resources()
{
    return [
        'res' => [
            'create' => [
                'callback' => 'custom_api_create_user',
                'access arguments' => array('administer access control'),
                'access arguments append' => FALSE,
                    'args' => [
                    [
                        'name' => 'data',
                        'type' => 'struct',
                        'description' => 'data',
                        'optional' => FALSE,
                        'source' => 'data'
                    ]
                ],
            ]
        ]
    ];
}

Here the node access function (empty) :

function custom_api_node_access($op, $data) { 
}

For Authentication I use HTTP basic authentication and/or Session authentication

I don't know what I have to do to limit access only for admin. I've tried a lot but never work with authentication.

I feel like it's very easy to use it, but I don't understand it.

I've already posted a message about this issue but differently. Can someone delete the first one, please ? How do I use the access arguments?


Edit

custom_api.module file :

             'create' => [
                'callback' => 'custom_api_create_users',
                'access callback' => 'custom_api_access_callback',
                'access arguments' => array('administer create field_projects'),
                'access arguments append' => TRUE,
                'access callback file' => array(
                  'type' => 'inc',
                  'module' => 'support',
                  'name' => 'custom_api.resource',
                ),
                    'args' => [
                    [
                        'name' => 'data',
                        'type' => 'struct',
                        'description' => 'data',
                        'optional' => FALSE,
                        'source' => 'data'
                    ]
                ]

custom_api.resource.inc :

function custom_api_access_callback($permission) {
  return user_access($permission);
}

Permission from Database, role_permission table :

Permission : access comments Module : comment

Permission : access content Module : node

Permission : access news feeds Module : aggregator

Permission : access site map Module : site_map

Permission : create field_credits Module : field_permissions

Permission : create field_credit_level_1 Module : field_permissions

Permission : create field_credit_level_2 Module : field_permissions

Permission : create field_credit_level_3 Module : field_permissions

Permission : create field_level Module : field_permissions

Permission : create field_number_of_credit_remove Module : field_permissions

Permission : create field_projects Module : field_permissions

Permission : create field_remove_credit Module : field_permissions

Permission : search content Module : search

Permission : subscribe to newsletters Module : simplenews

Permission : use advanced search Module : search

Permission : use custom search Module : custom_search

Permission : use custom search blocks Module : custom_search_blocks

Permission : use text format guestbook_txt Module : filter

Permission : access comments Module : comment

Permission : access content Module : node

Permission : access news feeds Module : aggregator

Permission : access site map Module : site_map

Permission : administer custom search Module : custom_search

Permission : administer custom search blocks Module : custom_search_blocks

Permission : administer search Module : search


I have link to add data (client and node : project and client) in drupal website. Only administrators can add data.

  • Why are you using hook_node_access for creating a user? – David Thomas Aug 8 '16 at 1:28
  • Actually, I create three entities "user", "client" and "project"... I thought to use this hook to limit access. – Swarovski Aug 8 '16 at 19:08
0

Drupal Services is a bit strange in that it doesn't seem to fall back to calling the user_access() function. Instead, I always provide a custom access callback function, and in it usually just make a call to user_access(). Try this:

'doli' => [
        'create' => [
            'callback' => 'custom_api_create_user',
            'access callback' => 'example_access_callback',
            'access arguments' => array('administer access control'),
            'access arguments append' => TRUE,
            'access callback file' => array(
              'type' => 'inc',
              'module' => 'example',
              'name' => 'example.resource',
            ),
                'args' => [
                [
                    'name' => 'data',
                    'type' => 'struct',
                    'description' => 'data',
                    'optional' => FALSE,
                    'source' => 'data'
                ]
            ],
        ]
    ]

Then make a file called example.resource.inc in your module's folder, and place this code into it:

function example_access_callback($permission) {
  return user_access($permission);
}
  • Thank you Tyler for your answer but it doesn't work, I got this message with postman tool : "Access denied for user Name". Let me know if you require any further information. – Swarovski Aug 9 '16 at 18:54
  • Please verify the machine name of the permission you are checking against. For example, I don't recognize the administer access control permission, which module provides that? – tyler.frankenstein Aug 10 '16 at 13:33
  • Yes, it's not administer access control but administrator. If I use "Session authentication" I got this message : "Access denied for user anonymous", if I use "HTTP basic authentication" I got : "Access denied for user Swarovski"... – Swarovski Aug 10 '16 at 15:59
  • no idea ? I got this error : 401 Unauthorized. I use administrator access content permission ... Don't know why it doesn't work. – Swarovski Aug 15 '16 at 12:30
  • The permission name is probably administer nodes and/or access content. – tyler.frankenstein Aug 15 '16 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.