How can i customize Wordpress list_users capability? - php

I am trying to find a way to customize the list_users capability in Wordpress,
I have created a new role called my_admin and i added some capabilities to it.
The problem is that the list_users capability displays all users while i want to display only a group of users according to a user meta field.
Another question about the edit_users capability can i limit this capability to edit the users without promoting them, this capability enables editing the users and also promoting them which means that it enables changes to user roles also. Is there any way i can remove this promoting feature from it, can i also select only one input field to be edited and remove other fields?
function my_new_service_capabilities() {
// Get the role object.
$editor = get_role( 'my_admin' );
// A list of capabilities to add.
$addcaps = array(
'read',
'edit_posts',
'list_users', //I need to list users according to a user meta value.
'edit_users', //I need to enable editing users in only one input field
'moderate_comments',
);
foreach ( $addcaps as $acap ) {
// Add the capability.
$editor->add_cap( $acap );
}
}
add_action( 'init', 'my_new_service_capabilities' );

I think this should work for you →
https://codex.wordpress.org/Class_Reference/WP_User_Query
steps →
Retrieve all users.
User For each loop.
Use meta_query parameter, because WP stores capabilities as json
string in user_meta.
I am giving you a clue how to do this →
$entire_user_list = get_users();
$somse_users= array();
foreach($entire_user_list as $user){
if($user->has_cap('specific_capability')){
$somse_users[] = $user;
}
}

I had solved it using the DOMDocument class.

Related

Cannot assign user roles in wordpress

I was unable to login to wp-admin "You don't have sufficient privileges" so I added this code to my themes functions.php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
function codelight_all_permissions( $allcaps, $cap, $args ) {
$allcaps[$cap[0]] = true;
return $allcaps;
}
add_filter( 'user_has_cap', 'codelight_all_permissions', 0, 3 );
Then I was able to get in as site admin, I went to users and noticed there are no roles assigned. In the database I have level 10 and a:1:{s:13:"administrator";s:1:"1";} assigned.
If I create a new user I cannot choose a role for that user from the dropdown. I only get the option "No role for this site". Does anyone know how I can troubleshoot this issue?

Magento - Differentiate Normal orders and orders created programatically

I am able to create orders programmatically using my custom module.
I wish to add a flag to differentiate between various types of orders that can be created from admin/websites and orders created from my custom module.
I understand that admin and website orders can be differentiated by
if(!empty($order->getRemoteIp()){
//place online
}
else{
// place by admin
}
However, I still want to differentiate between orders manually placed from admin and orders placed from my custom module.
The following are some solutions I thought of
1) Adding a prefix to the order or order-increment id.
2) Creating a new store during creation of module and add all orders from my custom module using that store. However, I am not sure of what implications this might cause.
3) I am able to change the store name during order creation using
$order->setStoreName('customName');
But, this is not visible in the admin grid or order detail page. I'm guessing they fetch the information for "Purchased From" from the store id.
I am looking for, what could be the best solution from the above, or a better solution if any.
Note: My module is currently compatible with magento v1.4 and above. So I will need a solution that covers most versions.
You could create an order attribute and set it when you create your order.
To create an order attribute you can do like this, in your module folder, in sql/mymodule_setup/mysql4-upgrade-VNUMBER.php:
$installer = $this;
$installer->startSetup ();
$setup = new Mage_Eav_Model_Entity_Setup ( 'core_setup' );
$installer->getConnection()
->addColumn (
$installer->getTable ( 'sales_flat_order' ),
'my_attribute_code', // this is where you set the attribute code
'varchar(255) DEFAULT NULL' );
$setup->startSetup ();
$setup->addAttribute (
'order',
'my_attribute_code',
array (
'type' => 'int', // or text or whatever
'label' => 'My attribute Label'
));
$installer->endSetup ();
Then when dealing with the order, simply use
$order->setMyAttributeCode($value);

How to remove unwanted roles from admin's "New User" Page?

Basiclly, I must Remove the next roles whenever the admin is creating a new user and clicks on the dropdown menu tu display all the available roles:
Contributor
Editor
Unapproved User
Author
It should only display Subscriber and Administrator.
Also I must change the name of Subscriber to something different, where is this code? This problem would seem to me like there was a plugin to it.
To remove a role you can use
<?php remove_role( 'editor'); ?>
and to rename a role add the following php t your functions.php file
function change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
$wp_roles->roles['administrator']['name'] = 'Owner';
$wp_roles->role_names['administrator'] = 'Owner';
}
add_action('init', 'change_role_name');
(The above is from here
There is a function called remove_role that WordPress has in its PHP API. you use it like this:
<?php remove_role('author'); ?>
author could be replaced with any role you want. The way you want to use this command is different than most WP commands. This is because it removes the role from the DB so any future times, the role is already removed. I would recommend making a custom WP plugin that just removes the roles you want on activation.WP Plugin Help

Limiting Taxonomy Terms in Dropdowns in Drupal content forms based on roles and user id

I am trying to find a way to limit Drupal Taxonomy Terms that appear on Content Add form based on their Role permissions but also on their user id. I have a taxonomy set called Departments (IT, Marketing, Accounting, etc).
On this specific content add form, there is a dropdown which asks you to assign the department that the content will belong to. I would like it to be the full dropdown list if the role is set as Admin however if the it is a regular user, I want it to instead be defaulted or locked to the department that they are assigned to in their user profile.
I looked around and have found multiple suggestions online such as setting up an entity reference field and using Taxonomy Access Control module but I can't seem to set it up correctly. Any suggestions?
Thanks!
I think the easiest way to achieve this would be a little code in a custom module.
First I would use hook_permisision to create a new permission called manually_set_department.
It is preferable to check for permissions not roles as roles can change and it is more flexible once a permission is set up as you can apply it to other roles if needed.
To do this use:
function MY_MODULE_permission() {
return array(
'manually_set_department' => array(
'title' => t('Manually set department'),
'description' => t('Allows users to manually set their department'),
),
);
}
Now we need to cut down the list of departments if the user does not have the above permission. To do this we can use hook_form_alter. In this example I'm assuming your content type is called page:
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'page_node_form':
// dpm($form); // dpm (supplied by the devel module) is always useful for inspecting arrays
if(!user_access('manually_set_department')) { // check the permission we created earlier
global $user;
$account = user_load($user->uid); // load the users account
//dpm($account);
$deparment_tid = $account->field_department[LANGUAGE_NONE][0]['tid']; // get their department tid
foreach($form['field_department'][LANGUAGE_NONE]['#options'] as $k => $v) {
if($k != $deparment_tid) unset($form['field_department'][LANGUAGE_NONE]['#options'][$k]); // unset any but their own departments
}
}
break;
}
}

SugarCRM GetAllRoles() to Retrieve Data for Custom Module/Page

I've been working with SugarCRM by creating a custom module and a custom PHP file to that module. I need to display all the Roles available to Users and have the data displayed on the custom PHP page.
I have looked in to getAllRoles() function and...
ACLRole::getAllRoles(boolean $returnAsArray=false);
Can any one help me get these functions to work properly?
(Please answer only if you know the answer and do not close the question with false reasons.)
You can easily get all Roles that aren't deleted, you will find the method in /modules/ACLRoles/ACLRole.php. It will return either an array of array representations of acl roles or an array of ACLRoles.
The query is:
'SELECT acl_roles.* FROM acl_roles WHERE acl_roles.deleted=0 ORDER BY name';
Try this code:
$roles = array();
$roles = ACLRole::getAllRoles(true);
print_r($roles);
sugar_die();
Hope that helps.

Categories