roles)) { return new WP_Error('wiaas_authentication_error', 'No permissions!'); } return $user_id; } /** * Authenticate wiaas user on login based on roles assigned to organization * * User role will be determined in `maybe_filter_user_roles` based on his organization roles * * If this is REST API login request user will be only authenticated with customer role if his organization his * customer role assigned to it. * * If this is backend login request user will be authenticated with previously selected or first available role * from his organization roles. * * @param WP_User $user * @return WP_User|WP_Error */ public static function authenticate_user_on_login($user) { // do nothing if there is an error already, // user is super admin if (is_wp_error($user) || $user->ID === 1) { return $user; } if (empty($user->roles)) { return new WP_Error('wiaas_authentication_error', 'No permissions!'); } return $user; } /** * * Override default user roles with only his organization roles. * * If this is REST API request retrieve customer role if organization has that role. * * If this is backend request retrieve previously selected role or first available organization role with * backend access. * * @param $null * @param int $user_id * @param string $meta_key * @return array|null */ public static function maybe_filter_user_roles($null, $user_id, $meta_key) { global $wpdb; if ($user_id !== 0 && $user_id !== self::SUPER_ADMIN_USER_ID && $meta_key === $wpdb->get_blog_prefix() . 'capabilities') { return array( array( 'customer' => true ) ); // import organization functions (during user authentication it is not yet loaded) require_once dirname( __FILE__ ) . '/user/wiaas-organization-functions.php'; // get user organization $organization_id = wiaas_get_user_organization_id($user_id); // validate if user has organization if ( empty( $organization_id) ) { return array(); } // get organization roles $roles = wiaas_get_organization_roles($organization_id); // if organization has no roles assigned to it user will have no roles if ( empty($roles) ) { return array(); } /** * REST API access */ // for REST API access allow only customer role for user if ( $is_rest_api = strpos($_SERVER['REQUEST_URI'], rest_get_url_prefix()) ) { return in_array('customer', $roles) ? array( array( 'customer' => true ) ) : array(); } /** * BACKEND ACCESS */ // remove customer role $roles = array_diff($roles, array( 'customer')); // not available backend roles for user if ( empty($roles) ) { return array(); } // retrieve selected role for user $role = get_user_meta($user_id, '_wiaas_current_user_admin_role', true); // if user has no selected role, selected role in invalid (deleted) or organization has no selected role // assign first available role to user if ( empty($role) || ! wp_roles()->is_role($role) || ! in_array($role, $roles) ) { // pick first role $role = $roles[0]; update_user_meta($user_id, '_wiaas_current_user_admin_role', $role); } return array( array ( "$role" => true )); } return null; } } Wiaas_Authentication::init();