I use Drupal 5.x (and cannot upgrade to 6.x until all modules are available in their 6.x version) and need a node type with custom fields.
The fields may be edited by some users (read-write access), only visible to some other users (read-only access) and not applicable to a third role of users (no access).
In addition, the management of the node type follows a custom workflow and access to the custom fields depends on the current state of the node.
Problem:
A user with read-write access to a CCK field can successfully set and save the value of that field, however, a user with read-only access to a CCK field CANNOT set and save the value of that field (value remains 'null'). This was tested on User Reference fields and may not be the case with other field types.
My specific case:
One of my node types has a User Reference CCK field which stores a link to the "owner" of that node. Some authorized users may create a node of that type and assign or edit the "owner" field freely, i.e. select any user at any point in the workflow.
Other restricted users may create nodes of that type but to them, the "owner" field is read-only and pre-populated with the currently logged in user (which should be the same as the node author).
What I did:
- I installed and enabled the modules CCK (to add custom fields such as User Reference) and Workflow (to manage states and field permissions depending on the state)
- I defined a new node type in Drupal's admin pages. One of the fields is the "owner" of type User Reference
- In the "owner" field, I defined custom PHP code to define its default value (the currently logged in user). See how and a related issue in #243340: Admin cannot set PHP code for default value. BTW, is there a better way to initialize a User Reference field to the currently logged in user other than defining custom PHP code?
- I defined access rights depending on state and role:
- Using the "Administer » Site building » Workflow Required Fields" admin menu - part of the Workflow module, I defined the "owner" field to be mandatory in all states.
- Using the "Administer » Site building » Workflow" admin menu (under the "Access control" fieldgroup) - part of the Workflow module, I defined which roles can view/edit/delete nodes depending on the state. One problem here is that Workflow doesn't allow to define access rights applicable during the creation state of the node. This is why...
- ...I added rows in the
workflow_fieldstable to define the access rights during the "(creation)" state (this is a pre-defined system state valid only during creation of a node). This is the only way I found to set some CCK fields to read-write/read-only/not applicable at creation time depending on user role. It seems the permissions applicable during the creation state may not be defined using the Workflow admin pages.
- In a new module, I defined a custom validation rule using the nodeapi hook, to ensure that unauthorized users can only define themselves as "owner", as follows (only the relevant bits).
... function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { global $user; switch ($op) { case 'validate': if ($node->type == "mytype") { if(!user_access('full access to mytype') && $node->field_owner[0][uid] != $user->uid) { form_set_error('field_owner_uid', t('You are not authorized')); } } break; } }
During creation of a node and first display, the "owner" is displayed with the correct value and access rights, i.e. authorized users may edit the "owner", restricted users may only view it. The initial value is the currently logged in user as expected.
At this stage, everything is OK irrespective of user role and access rights.
Problem:
When submitting the node as a restricted user with read-only access to the "owner" field, the "owner" field ($node->field_owner[0][uid]) is 'null' instead of the uid of the currently logged in user, causing my nodeapi validation rule to fail.
Question:
How can I set and save the value of a read-only User Reference field in Drupal 5.x (I cannot upgrade yet)?
Comments
Comment #1
pete-drupal commentedI'm trying to reproduce with as few enabled modules as possible. So far I came up with the following list:
(Package / Module)
Comment #2
karens commentedClosing old issues. None of us is using the D5 version any more, so hard to provide any support. Sorry.