Enable login by role both on frontend and backend

This commit is contained in:
Almira Krdzic
2018-10-22 09:47:48 +02:00
parent 87f408125d
commit 21fafc5a43
20 changed files with 618 additions and 367 deletions

View File

@@ -38,22 +38,22 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
add_action( 'pre_delete_term', array( __CLASS__, 'on_taxonomy_term_will_be_deleted' ), 10, 2);
add_action( 'delete_' . self::TAXONOMY_NAME, array( __CLASS__, 'on_organization_deleted' ));
add_action('acf/save_post', array(__CLASS__, 'on_organization_roles_maybe_updated'));
add_action('acf/save_post', array(__CLASS__, 'on_organization_roles_maybe_updated'), 20);
add_action('set_object_terms', array( __CLASS__, 'on_taxonomy_term_assigned' ), 10, 4);
add_action('deleted_term_relationships', array( __CLASS__, 'on_taxonomy_term_unassigned' ), 10, 3);
// Remove bulk editor for organizations on users list
remove_filter( 'admin_notices', array( $this, 'bulk_notice'));
remove_filter( 'bulk_actions-users', array( $this, 'bulk_actions'));
remove_filter( 'bulk_actions-users', array( $this, 'bulk_actions_sort'));
remove_action( 'handle_bulk_actions-users', array( $this, 'handle_bulk_actions'));
remove_filter( 'admin_notices', array( $this, 'bulk_notice'));
remove_filter( 'bulk_actions-users', array( $this, 'bulk_actions'));
remove_filter( 'bulk_actions-users', array( $this, 'bulk_actions_sort'));
remove_action( 'handle_bulk_actions-users', array( $this, 'handle_bulk_actions'));
// remove default organization info from profiles
// it will be handled by custom fields
remove_action( 'show_user_profile', array( $this, 'edit_user_relationships' ), 99);
remove_action( 'edit_user_profile', array( $this, 'edit_user_relationships' ), 99 );
// remove default organization info from profiles
// it will be handled by custom fields
remove_action( 'show_user_profile', array( $this, 'edit_user_relationships' ), 99);
remove_action( 'edit_user_profile', array( $this, 'edit_user_relationships' ), 99 );
}
// hooks functions
@@ -61,12 +61,16 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
/**
* Creates corresponding access group for newly created organizational term
*
* @param $organization_id id of the organization term
* @param int $organization_id id of the organization term
*/
public static function on_organization_added($organization_id) {
self::_create_organization_access_group($organization_id);
do_action('wiaas_organization_created', $organization_id);
$roles = wiaas_get_organization_roles($organization_id);
self::_assign_organization_roles_capabilities($organization_id, $roles);
}
/**
@@ -104,6 +108,11 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
//get organization id
$id = absint(str_replace('term_', '', $id));
if ($id) {
self::_assign_organization_roles_capabilities($id, $roles);
}
do_action('wiaas_organization_roles_updated', $id, $roles);
}
}
@@ -153,7 +162,7 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
*/
public static function get_user_organization_id($user_id = null) {
if (!isset($user_id)) {
$user_id = get_current_user_id();
$user_id = get_current_user_id();
}
$organization_id = get_user_meta($user_id, '_wiaas_organization_id', true);
@@ -284,4 +293,33 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
}
}
}
/**
*Reflect organization roles in access group
*
* This will be used when assigning roles to user
*
* @param int $organization_id
* @param array $roles
*/
private static function _assign_organization_roles_capabilities($organization_id, $roles) {
$access_group_id = self::_get_organization_access_group_id($organization_id);
$all_roles = array( 'commercial_lead', 'supplier', 'customer', 'administrator');
foreach ($all_roles as $role) {
$cap = Groups_Capability::read_by_capability('wiaas_' . $role);
Groups_Group_Capability::delete($access_group_id, $cap->capability_id);
}
foreach ($roles as $role) {
$cap = Groups_Capability::read_by_capability('wiaas_' . $role);
Groups_Group_Capability::create(array(
'group_id' => $access_group_id,
'capability_id' => $cap->capability_id
));
}
}
}