How to enable administrator role - drupal

I have Drupal 7.22 with "minimal" install profile.
When I create new user, I have disabled option in "Administration role".
How can I enable administrator role for new user?

Create at least one role (maybe call it "Administrator") and it will show up in the dropdown. (The 2 default roles don't count)

you can create anew role and give all privilege (edit,add,remove ..Etc) and give it to new user

Execute this PHP code in the devil module (taken from the file \drupal-site\profiles\standard\standard.install):
// Create a default role for site administrators, with all available permissions assigned.
$admin_role = new stdClass();
$admin_role->name = 'administrator';
$admin_role->weight = 2;
user_role_save($admin_role);
user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));
// Set this as the administrator role.
variable_set('user_admin_role', $admin_role->rid);
// Assign user 1 the "administrator" role.
db_insert('users_roles')
->fields(array('uid' => 1, 'rid' => $admin_role->rid))
->execute();

Related

Wordpress user role, that can only edit users with another user role?

I created a new user role, but I need this user role to only be able to edit users with another user role, I do not want this new user role to be able to edit users like, editors, author, etc...
Ideas?
you can add roles so:
$result = add_role(
'editor_of_users',
__( 'Editor of Users' ),
array(
'edit_users' => true,
// Add more capabilities...
)
);
if ( null !== $result ) {
echo 'Yay! New role created!';
}
else {
echo 'Oh... the editor_of_users role already exists.';
}
For more information you can read the documentation about: add_role
And the all users capabilities here: Roles and Capabilities
EDIT:
Read this post: to know how add to users the capabilities to edit specific roles...
How to allow an user role to create a new user under a role which lower than his level only?
I hope you resolve it... Good Look! ;)

how customize drupal 7 for multi user instance

When generating a new drupal site (multisite), I would like to define the default user profile as an admin user without acces to administration menu.
How can I create this new user as an install profile with default login infos, keeping at the same time the admin user?
i found this node ,but i can't understand ! how can I apply !?
Thank you for suggestions.
This tutorial lays out how to create roles and accounts in an install profile.
The core idea is to programmatically create a new role and user account in the profile .install file. For example (untested):
// Create a role for site content managers
$admin_role = new stdClass();
$admin_role->name = 'content manager';
$admin_role->weight = 2;
user_role_save($admin_role);
// Grant all admin permissions
user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));
// Revoke the toolbar permissions
user_role_revoke_permissions($admin_role->rid, array(
'access administration menu', // The admin_menu module toolbar
'access toolbar', // The core toolbar
));
// Create a user with the content manager role
$new_user = array();
$new_user['name'] = 'manager';
$new_user['mail'] = 'manager#example.com';
$new_user['roles'] = array($admin_role->rid => $admin_role->rid);
$new_user['pass'] = 'incredibly-secure-password-here';
$new_user['status'] = 1;
$account = user_save(drupal_anonymous_user(), $new_user);

Can I temporarily change a user's role in Wordpress?

I need to grant users a specific role (Editor, Administrator, etc.) along with all its capabilities on the fly in Wordpress, but I don't want to update their role in the database (so that the next time they come back, they will have their original role). How can I go about doing this?
Here's what I ended up doing:
add_filter( 'user_has_cap', 'override_caps' );
function override_caps($allcaps){
if( ... ){ // When to override caps
$role_name = 'administrator';
$role = get_role($role_name); // Get the role object by role name
$allcaps = $role->capabilities; // Get the capabilities for the role
$allcaps[$role_name] = true; // Add role name to capabilities
}
return $allcaps;
}

How to: Unpublish Nodes by Author when Author is assigned Role

I'm using Drupal 7 + Rules. I would like to create a rule that unpublishes all nodes authored by a user when they have been given a particular role.
EVENT - After updating an existing user account
CONDITION - User has role(s): SelectedRole
ACTION - ???
BONUS: If this could be limited to nodes of a certain type, that would be even better.
If there is a better way outside of Rules to do this, I'm open to other ideas.
Thanks so much!
You can create a custom ruleset to loop through the nodes or a Views Bulk Operation action.
An easiest option is to add a custom PHP function on your rule (PHP > Execute custom PHP code). Of course you have to enable the php filter core module if you didn't already.
In the PHP action you have to get all the nids of the published nodes the current user and loop through them to unpublish them. I will use the EntityFieldQuery API class but you can use the database functions also.
// Get updated user id
$uid = $account -> uid;
// Get all nodes from user that are of NODE_TYPE and are published
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'NODE_TYPE')
->propertyCondition('status', 1)
->propertyCondition('uid', $uid);
$result = $query->execute();
$nids = array_keys($result['node']);
// Load all nodes in one go for better performance.
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
// set status property to 0 (unpublished)
$node->status = 0;
// re-save the node
node_save($node);
}
I would also suggest to add one more Condition for the user before the one you are using: User Has Roles: (NOT) SelectedRole so that the action do not run everytime the user profile is updated.
References:
Use Rules to Unpublish content based on Author's role
Publish unpublished node programmatically
Find nodes created by user (more programmatically)

Change node author automatically

Anonymous user is able to post nodes. After posting node, user is redirected to registration. After registration, the previously submitted node should be linked with newly registered user.
I played with rules and entities but I was not able to get it work properly. Any ideas?
I would write a custom module (but that's me). The module needs to implement hook_node_insert and save the nid into SESSION. Then on hook_user_insert it can do the change. Untested code:
function foo_node_insert($node) {
$_SESSION['mynodes'][] = $node->nid;
}
function foo_user_insert($edit, $account) {
if (!empty($_SESSION['mynodes'])) {
foreach ($_SESSION['mynodes'] as $nid) {
$node = node_load($nid);
$node->uid = $account->uid;
// This saves the revision as the current user uid but that's just what we wanted.
node_save($node);
}
}
}
Edit: don't forget unset($_SESSION['mynodes']);
Save the node data until after registration and post it then.
There's the Anonymous Node Create module.
The module allows anonymous users to create nodes. But 'anonymous' is questionable in this module. This module alters the node form for anonymous users by adding two field groups at the end before the save button.
The first field group has fields that allow users to create a new account. This new account is then the author of the new node created.

Resources