set_role(''); return new WP_Error('wiaas_authentication_error', 'No set permissions!', array( 'status' => 403, )); } // authenticate valid admin panel user $current_user->set_role($role); return $user_id; } /** * Authenticate wiaas user on login based on roles assigned to organization * * If this is non admin panel request authenticate user if he can be customer. * * If this is admin panel 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 === self::SUPER_ADMIN_USER_ID ) { return $user; } $rest_api_slug = rest_get_url_prefix(); $valid_api_uri = strpos($_SERVER['REQUEST_URI'], $rest_api_slug); /** * CUSTOMER API AUTHENTICATION */ // validate customer user login if ($valid_api_uri) { $role = user_can($user->ID, 'wiaas_customer') ? 'customer' : ''; $user->set_role($role); return empty($role) ? new WP_Error('wiaas_authentication_error', 'No Customer permissions!', array( 'status' => 403, )) : $user; } /** * ADMIN PANEL AUTHENTICATION */ // retrieve selected role for user $role = get_user_meta($user->ID, '_wiaas_admin_role', true); // if user has selected role then use it if (! empty($role) && user_can($user->ID, 'wiaas_' . $role)) { return $user; } // user does not have selected role so try to assign one in order of access if (user_can($user->ID, 'wiaas_administrator')) { $role = 'administrator'; } else if (user_can($user->ID, 'wiaas_commercial_lead')) { $role = 'commercial_lead'; } else if (user_can($user->ID, 'wiaas_supplier')) { $role = 'supplier'; } if (empty($role)) { return new WP_Error('wiaas_authentication_error', 'No permissions!', array( 'status' => 403, )); } update_user_meta($user->ID, '_wiaas_admin_role', $role); $user->set_role($role); return $user; } /** * validate that successfully logged in rest api user can be customer * * @param array $data * @param WP_User $user * @return array | WP_Error * */ public static function authenticate_rest_user_on_login($data, $user) { // if admin do nothing if ($user->ID === self::SUPER_ADMIN_USER_ID) { return $data; } $role = user_can($user->ID, 'wiaas_customer') ? 'customer' : ''; $user->set_role($role); return empty($role) ? new WP_Error('wiaas_authentication_error', 'No Customer permissions!', array( 'status' => 403, )) : $data; } } Wiaas_Authentication::init();