Download & Extend

Programmatically adjusting permissions

Project:Content Access
Version:5.x-1.4
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

Consider the following...

I have a CCK content type which includes a user select. When a user creates a node of this type, he/she has to pick a user from the list to which to grant access rights. When the node is submitted, I would like to run some code that grabbed that new $nid, pulled the user selected from the db, and then grant access.

Now I know this question probably doesn't relate directly to the Content Access module but I thought this would be a good place to ask.

Any ideas how this might be achieved?

Cheers,
Chris.

Comments

#1

#2

Any solution that works without a module?

#3

The trick is look inside of drupal core functions for the pieces you need, then take them out and run them in a modules install file, or even once in your theme template.php file

Specifically, if you can practice some simple mysql queries, you'll find a lot of power to quickly do tasks.

<?php
/*
*
* Programmatically create a role in Drupal
*
*/
db_query("INSERT INTO {role} (name) VALUES ('%s')", 'ecoach');

/*
*
*  assign role to newly created user accounts
*
*/
// ge the role id number ("rid")
$rid = db_result(db_query("SELECT rid FROM role WHERE name = 'ecoach'"));


$query = db_query("SELECT uid FROM `users` WHERE `name` LIKE '%Edge Studio Voice Over Coach%'");
while (
$uid = db_result($query)) {
   
// check to make sure you are not adding role twice
       
$count = (int)db_result(db_query("SELECT COUNT(*) AS COUNT FROM {users_roles} WHERE uid = %d AND rid = %d",$uid,$rid));
   
       if(
$count < 1) {
               
// add role programmatically to user
       
db_query("INSERT INTO {users_roles} (rid, uid) VALUES ('%d','%d')", $rid, $uid);
       
    }
   
// if you want to assign permissions to your role just add them as a comma separated list
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, 'edit field_ecoach_feedback, edit field_my_secret_field');
?>