Merge branch 'organization-access' into 'master'

Organization access

See merge request saburly/wiaas/new-wiaas!32
This commit was merged in pull request #32.
This commit is contained in:
Bilal Catic
2018-10-14 22:27:52 +00:00
31 changed files with 1353 additions and 126 deletions

View File

@@ -2,6 +2,8 @@ FROM php:7.0-apache
ARG API_URL
ARG WIAAS_CUSTOMER_INTERFACE
ARG MYSQL_DATABASE
ARG MYSQL_USER
ARG MYSQL_PASSWORD
@@ -35,6 +37,8 @@ ENV WP_LOGGED_IN_SALT ${WP_LOGGED_IN_SALT}
ENV WP_NONCE_SALT ${WP_NONCE_SALT}
ENV WP_JWT_AUTH_SECRET_KEY ${WP_JWT_AUTH_SECRET_KEY}
ENV WIAAS_CUSTOMER_INTERFACE ${WIAAS_CUSTOMER_INTERFACE}
RUN apt-get update && apt-get install -y git unzip gnupg mysql-client sudo
COPY docker/php/composer.phar /usr/local/bin/composer

View File

@@ -0,0 +1,17 @@
#wpadminbar #wp-admin-bar-wiaas-view-as .ab-icon {
float: none;
margin-left: 10px;
color: rgba(240,245,250,0.7) !important;
}
#wpadminbar #wp-admin-bar-wiaas-view-as .ab-label {
font-weight: bold;
}
#wpadminbar #wp-admin-bar-wiaas-view-as a:not(:hover) .ab-icon:before {
color: rgba(240,245,250,0.7) !important;
}
#createuser .acf-taxonomy-field, #your-profile .acf-taxonomy-field {
width: 25em;
}

View File

@@ -120,4 +120,12 @@
#wiaas_package_option_groups_list .wiaas_option_group h3 {
background-color:#F9F9F9;
}
#createuser label[for="role"], #createuser label[for="url"], #createuser #role, #createuser #url{
display: none;
}
#your-profile label[for="role"], #your-profile label[for="url"],#your-profile #role, #your-profile #url{
display: none;
}

View File

@@ -0,0 +1,160 @@
<?php
class Wiaas_Admin_Organization {
public static function init() {
// role switcher for organization
add_action( 'admin_bar_menu', array( __CLASS__, 'add_role_switcher_menu' ), 10, 2 );
// change user role if requested
add_action('admin_init', array(__CLASS__, 'maybe_change_user_role'));
add_filter('manage_users_columns', array(__CLASS__, 'manage_users_table_columns'));
add_filter('views_users', array(__CLASS__, 'hide_users_table_list_navigation'));
add_filter('get_role_list', array(__CLASS__, 'get_role_list_for_user'), 10, 2);
add_filter('woocommerce_customer_meta_fields', array(__CLASS__, 'hide_woocommerce_customer_fields'));
}
public static function hide_woocommerce_customer_fields() {
return array();
}
public static function hide_users_table_list_navigation() {
return array();
}
public static function get_role_list_for_user($role_list, $user) {
$organization_id = wiaas_get_user_organization_id($user->ID);
if (! empty($organization_id)) {
$organization_roles = wiaas_get_organization_roles($organization_id);
foreach ($organization_roles as $organization_role) {
$role_list[$organization_role] = translate_user_role( wp_roles()->role_names[ $organization_role ] );
}
if (! empty($organization_role)) {
unset($role_list['none']);
}
}
return $role_list;
}
public static function manage_users_table_columns( $defaults = array() ) {
$defaults['role'] = __('Roles', 'wiaas');
unset($defaults['posts']);
return $defaults;
}
/**
* Adds organization roles switcher menu to admin bar
*
* @param $admin_bar
*/
public static function add_role_switcher_menu($admin_bar) {
if (is_super_admin()) {
$roles = array( 'administrator' );
} else {
$organization_id = wiaas_get_current_user_organization_id();
$roles = wiaas_get_organization_roles($organization_id);
}
$user = wp_get_current_user();
$current_role = $user->roles[0];
// Add menu item.
$admin_bar->add_menu( array(
'id' => 'wiaas-view-as',
'parent' => 'top-secondary',
'title' => '<span>' .
__( 'View as:', 'wiaas' ) .
'</span>' .
'<span style="margin-left: 6px; font-weight: bold;">'.
__(translate_user_role( wp_roles()->role_names[ $current_role ] ), 'wiaas') .
'</span>' .
'<i style="float: right;" class="ab-icon dashicons dashicons-arrow-down"></i>',
'href' => false,
'meta' => array(
'title' => __( 'View As', 'wiaas' ),
'tabindex' => '0',
),
) );
foreach ($roles as $role) {
$role_name = translate_user_role( wp_roles()->role_names[ $role ] );
if ($role === $current_role) {
// highlight current role
$title = '<span class="ab-label">' . __( $role_name, 'wiaas' ) . '</span>';
$url = false;
} else if ($role === 'customer') {
// set external link for customer role
$title = '<span>' . __( $role_name, 'wiaas' ) . '</span>';
$title .= '<span class="ab-icon dashicons dashicons-external"></span>';
$url = WIAAS_CUSTOMER_INTERFACE;
$target = '_blank';
} else {
$title = '<span>' . __( $role_name, 'wiaas' ) . '</span>';
$url = wp_nonce_url(admin_url(), 'wiaas-set-role', 'wiaas-set-role-nonce');
$url = add_query_arg( 'wiaas-role', $role, $url );
$target = '_parent';
}
$admin_bar->add_menu( array(
'id' => 'wiaas_'.$role,
'parent' => 'wiaas-view-as',
'title' => $title,
'href' => $url,
'meta' => array(
'title' => __( $role_name, 'wiaas' ),
'target' => $target
),
) );
}
}
/**
*
* Process organization role switch request if needed
*
*/
public static function maybe_change_user_role() {
if (! empty($_GET['wiaas-set-role-nonce']) && ! empty($_GET['wiaas-role'])) {
if (wp_verify_nonce($_GET['wiaas-set-role-nonce'], 'wiaas-set-role')) {
$role_name = sanitize_key($_GET['wiaas-role']);
$role = wp_roles()->get_role($role_name);
if (! empty($role) && $role_name !== 'customer') {
// get current user
$current_user = wp_get_current_user();
update_user_meta($current_user->ID, '_wiaas_current_user_admin_role', $role->name);
// switch user role
$current_user->set_role($role->name);
}
}
wp_safe_redirect( admin_url('index.php') );
exit;
}
}
}
Wiaas_Admin_Organization::init();

View File

@@ -17,7 +17,17 @@ class Wiaas_Admin {
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-order-projects.php';
// Admin documents
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-documents.php';
// Admin organization interface
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-organization.php';
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
}
public static function enqueue_scripts() {
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
wp_enqueue_style( 'wiaas_admin_menu', $plugin_url . '/assets/css/menu.css' );
}
}
Wiaas_Admin::init();

View File

@@ -0,0 +1,200 @@
<?php
/**
* Handles user authentication for wiaas
*
* User roles are inherited from organization
*
* Class Wiaas_Authentication
*/
class Wiaas_Authentication {
const SUPER_ADMIN_USER_ID = 1;
public static function init() {
// authenticate current user
add_action('determine_current_user', array(__CLASS__, 'authenticate_current_user'), 999);
// authenticates user on login
add_filter( 'authenticate', array( __CLASS__, 'authenticate_user_on_login' ), 999, 3);
// retrieve preferred user role for user
add_filter('get_user_metadata', array(__CLASS__, 'maybe_filter_user_roles'), 10, 3);
// redirect to dashboard after login
add_filter( 'login_redirect', array( __CLASS__, 'login_redirect' ) );
// add role selector to login form
add_action('login_form', array(__CLASS__, 'pick_role_on_login'));
}
/**
* Redirect admin user to dashboard
*
* @return string
*/
public static function login_redirect() {
return admin_url('index.php') ;
}
/**
* Add role selector to login form
*/
public static function pick_role_on_login() {
?>
<p>
<label for="user_role"><?php esc_html_e( 'Role' , 'wiaas'); ?><br />
<select id="user_role" class="input" name="role">
<option value="administrator"><?php esc_html_e('Administrator', 'wiaas') ?></option>
<option value="supplier"><?php esc_html_e('Supplier', 'wiaas') ?></option>
<option value="commercial_lead"><?php esc_html_e('Commercial Lead', 'wiaas') ?></option>
</select>
</p>
<?php
}
/**
* Authenticate current user based on roles assigned to organization
*
* @param int|false $user_id
* @return int|false|WP_Error
*/
public static function authenticate_current_user($user_id) {
// do nothing if user not authenticated, user is super admin or this is REST API request
if (! $user_id || $user_id === self::SUPER_ADMIN_USER_ID) {
return $user_id;
}
$is_rest_api = strpos($_SERVER['REQUEST_URI'], rest_get_url_prefix());
$role = $is_rest_api ?
'customer' :
get_user_meta($user_id, '_wiaas_current_user_admin_role', true);
$result = self::_can_user_have_role($user_id, $role, $is_rest_api);
if (is_wp_error($result)) {
return false;
}
return $user_id;
}
/**
* Authenticate wiaas user on login based on roles assigned to organization
*
* @param WP_User $user
* @return WP_User|WP_Error
*/
public static function authenticate_user_on_login($user) {
// check if rest request
$is_rest_api = strpos($_SERVER['REQUEST_URI'], rest_get_url_prefix());
// do nothing if there is an error already,
// user is super admin or
// this is rest request
if (is_wp_error($user) || $user->ID === 1 || $is_rest_api) {
return $user;
}
if (empty($_POST['role'])) {
return new WP_Error('error', 'You must selected role to login!');
}
// get selected role
$requested_role = sanitize_key($_POST['role']);
// validate can user have requested role
$result = self::_can_user_have_role($user->ID, $requested_role, false);
// if user organization has no requested role prevent access
if (is_wp_error($result)) {
return $result;
}
// remember role for user and continue
update_user_meta($user->ID, '_wiaas_current_user_admin_role', $requested_role);
return $user;
}
/**
*
* Filters user roles retrieval so that selected user role is retrieved for admin panel
* and customer role is retrieved for JSON API request
*
* @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') {
$is_rest_api = strpos($_SERVER['REQUEST_URI'], rest_get_url_prefix());
$role = $is_rest_api ?
'customer' :
get_user_meta($user_id, '_wiaas_current_user_admin_role', true);
return array( array ( "$role" => true ));
}
return null;
}
// PRIVATE
/**
* Determines if user can have requested role based on his organization roles
*
* @param int $user_id
* @param string $user_role
* @param bool $is_rest_api
* @return bool|WP_Error
*/
private static function _can_user_have_role($user_id, $user_role, $is_rest_api) {
// check if role valid for access
if (! wp_roles()->is_role($user_role)) {
return new WP_Error('error', 'Role is not valid!');
}
// only customer role can access API
if ($is_rest_api && $user_role !== 'customer') {
return new WP_Error('error', 'No access!');
}
// customer role cannot access admin backend
if (! $is_rest_api && $user_role === 'customer') {
return new WP_Error('error', 'No access!');
}
// 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 new WP_Error('error', 'Account not completed!');
}
// get organization roles
$roles = wiaas_get_organization_roles($organization_id);
// validate if user has organization roles
if (!in_array($user_role, $roles)) {
return new WP_Error( 'error', 'Your account is not authorized for requested role. Please contact us for help.' );
}
return true;
}
}
Wiaas_Authentication::init();

View File

@@ -13,7 +13,11 @@ class Wiaas_DB_Update {
'20180826153509' => 'wiaas_create_broker_access_group',
'20180911101010' => 'wiaas_db_setup_exclusive_taxonomies',
'20180912101010' => 'wiaas_db_setup_default_cl',
'20181003164100' => 'wiaas_db_setup_customer_capabilities'
'20181003164100' => 'wiaas_db_setup_customer_capabilities',
'201810031644700' => 'wiaas_db_update_create_default_roles',
'201810101644700' => 'wiaas_db_import_aam_role_settings',
'201810111644700' => 'wiaas_db_update_add_organization_info_ui_fields',
'201810121644700' => 'wiaas_db_update_add_user_organization_ui_fields'
);
public static function execute() {

View File

@@ -157,15 +157,11 @@ class Wiaas_Pricing {
* Determines if customer and commercial lead are in the same company
* For now this is hardcoded and we have only one CL
*
* TODO: This should be changed after customer leads are handled
* TODO: This will be handled in next PR for setting commercial lead prices
* @return bool
*/
private static function _is_customer_same_company_as_cl() {
$current_user = wp_get_current_user();
$user_organization = Wiaas_User_Organization::get_user_organization($current_user->ID);
$is_same_company_as_cl = $user_organization->name === self::COMMERCIAL_LEAD_NAME;
return $is_same_company_as_cl;
return false;
}
/**

View File

@@ -21,6 +21,7 @@ class Wiaas_User {
public static function load_user_organization() {
if (class_exists('WP_User_Taxonomy')) {
require_once dirname( __FILE__ ) . '/user/class-wiaas-user-organization.php';
require_once dirname( __FILE__ ) . '/user/wiaas-organization-functions.php';
new Wiaas_User_Organization();
}

View File

@@ -33,7 +33,9 @@ class Wiaas_CLI_Update_DB_Command {
$wpdb->hide_errors();
include_once WIAAS_DIR . '/includes/class-wiaas-db-update.php';
include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-functions.php';
include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-general.php';
include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-roles.php';
include_once WIAAS_DIR . '/includes/db-updates/wiaas-db-update-ui-fields.php';
$pending_db_updates = Wiaas_DB_Update::get_pending_db_updates();

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,90 @@
[
{
"key": "group_5bbe558ba265c",
"title": "Organization Info",
"fields": [
{
"key": "field_5bbe5c0d1f1a7",
"label": "Phone",
"name": "_wiaas_organization_phone",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_5bbe5c291f1a8",
"label": "VAT Code",
"name": "_wiaas_organization_vat",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_5bbe559d66d17",
"label": "Roles",
"name": "_wiaas_organization_roles",
"type": "select",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"administrator": "Administrator",
"supplier": "Supplier",
"commercial_lead": "Commercial Lead",
"customer": "Customer"
},
"default_value": [],
"allow_null": 0,
"multiple": 1,
"ui": 1,
"ajax": 1,
"return_format": "label",
"placeholder": ""
}
],
"location": [
[
{
"param": "taxonomy",
"operator": "==",
"value": "wiaas-user-organization"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "seamless",
"label_placement": "top",
"instruction_placement": "field",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]

View File

@@ -0,0 +1,52 @@
[
{
"key": "group_5bbce9f38f149",
"title": "User Organization",
"fields": [
{
"key": "field_5bbe54366aac1",
"label": "Organization",
"name": "_wiaas_organization_id",
"type": "taxonomy",
"instructions": "Select Organization for user",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"taxonomy": "wiaas-user-organization",
"field_type": "select",
"allow_null": 0,
"add_term": 0,
"save_terms": 1,
"load_terms": 1,
"return_format": "id",
"multiple": 0
}
],
"location": [
[
{
"param": "user_form",
"operator": "==",
"value": "edit"
},
{
"param": "current_user_role",
"operator": "==",
"value": "administrator"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "seamless",
"label_placement": "left",
"instruction_placement": "field",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]

View File

@@ -1,9 +1,9 @@
<?php
/**
* Wiaas DB updates
* General Wiaas DB updates
*
* Functions for updating database to latest version
* General functions for updating wiaas database to latest version
*/

View File

@@ -0,0 +1,139 @@
<?php
/**
* Roles Wiaas DB updates
*
* Functions for updating wiaas roles in database
*
*/
function wiaas_db_update_create_default_roles() {
$roles = array(
'customer', // this role will be created by woocommerce so we do not have to create it here
'supplier',
'commercial_lead'
);
// remove wordpress default roles
remove_role('author');
remove_role('contributor');
remove_role('editor');
remove_role('subscriber');
// remove woocoommerce shop manager role
remove_role('shop_manager');
// Dummy gettext calls to get strings in the catalog.
/* translators: user role */
_x( 'Supplier', 'User role', 'wiaas' );
/* translators: user role */
_x( 'Commercial Lead', 'User role', 'wiaas' );
// Add wiaas roles
add_role(
'commercial_lead',
'Commercial Lead',
array(
'read',
'view_admin_dashboard'
)
);
add_role(
'supplier',
'Supplier',
array(
'read',
'view_admin_dashboard'
)
);
add_role(
'user',
'User',
array(
'read'
)
);
// set default wiaas role
update_option('default_role', 'user');
// capabilities
$capabilities = array();
$capability_types = array( 'wiaas_doc' );
foreach ( $capability_types as $capability_type ) {
$capabilities[ $capability_type ] = array(
// Post type.
"edit_{$capability_type}",
"read_{$capability_type}",
"delete_{$capability_type}",
"edit_{$capability_type}s",
"edit_others_{$capability_type}s",
"publish_{$capability_type}s",
"read_private_{$capability_type}s",
"delete_{$capability_type}s",
"delete_private_{$capability_type}s",
"delete_published_{$capability_type}s",
"delete_others_{$capability_type}s",
"edit_private_{$capability_type}s",
"edit_published_{$capability_type}s",
// Terms.
"manage_{$capability_type}_terms",
"edit_{$capability_type}_terms",
"delete_{$capability_type}_terms",
"assign_{$capability_type}_terms",
);
}
foreach ( $capabilities as $cap_group ) {
foreach ( $cap_group as $cap ) {
wp_roles()->add_cap( 'administrator', $cap );
wp_roles()->add_cap( 'commercial_lead', $cap );
}
}
}
function wiaas_db_import_aam_role_settings() {
// update role settings that are imported
$content = file_get_contents( dirname( __FILE__ ) . '/data/aam-settings.json' );
$importer = new AAM_Core_Importer($content);
$importer->run();
// update toolbar settins manually since it does not get imported
update_option('aam_toolbar_default', array (
'about' => '1',
'toolbar-wp-logo' => '1',
'wporg' => '1',
'documentation' => '1',
'support-forums' => '1',
'feedback' => '1',
'toolbar-site-name' => '0',
'view-site' => '1',
'view-store' => '1',
'toolbar-updates' => '1',
'toolbar-comments' => '1',
'toolbar-new-content' => '1',
'new-post' => '1',
'new-media' => '1',
'new-page' => '1',
'new-product' => '1',
'new-shop_order' => '1',
'new-shop_coupon' => '1',
'new-wiaas_doc' => '1',
'new-user' => '1',
'gravityforms-new-form' => '1',
)
);
}

View File

@@ -0,0 +1,19 @@
<?php
function wiaas_db_update_add_organization_info_ui_fields() {
$ui_json = file_get_contents( dirname( __FILE__ ) . '/data/wiaas-ui-field-organization-info.json' );
$ui_json = json_decode( $ui_json, true );
acf_import_field_group($ui_json[0]);
}
function wiaas_db_update_add_user_organization_ui_fields() {
$ui_json = file_get_contents( dirname( __FILE__ ) . '/data/wiaas-ui-field-user-organization.json' );
$ui_json = json_decode( $ui_json, true );
acf_import_field_group($ui_json[0]);
}

View File

@@ -351,13 +351,13 @@ class Wiaas_Document {
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array('slug' => 'wiaas_doc', 'with_front' => false),
'capability_type' => 'post',
'capability_type' => 'wiaas_doc',
'has_archive' => false,
'hierarchical' => false,
'taxonomies' => array(),
'menu_icon' => 'dashicons-media-document',
'exclude_from_search' => false,
'supports' => array('title')
'supports' => array('title'),
);
@@ -390,6 +390,12 @@ class Wiaas_Document {
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'wiaas_doc_type' ),
'capabilities' => array(
'manage_terms' => 'manage_wiaas_doc_terms',
'edit_terms' => 'edit_wiaas_doc_terms',
'delete_terms' => 'delete_wiaas_doc_terms',
'assign_terms' => 'assign_wiaas_doc_terms',
),
);
register_taxonomy( 'wiaas_doc_type', array( 'wiaas_doc' ), $args );

View File

@@ -48,8 +48,16 @@ class Wiaas_Product_Supplier {
*/
public static function on_organization_added($organization_id) {
$organization = get_term_by('id', $organization_id, 'wiaas-user-organization');
$supplier = wp_insert_term($organization->name, 'supplier');
add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
$supplier = term_exists($organization->slug, 'supplier');
if (! $supplier) {
$supplier = wp_insert_term($organization->name, 'supplier', array(
'slug' => $organization->slug
));
}
add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
}
}

View File

@@ -32,13 +32,24 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
parent::hooks();
add_action('user_new_form', array( $this, 'show_organizations_selection' ));
add_action('user_register', array( $this, 'save_terms_for_user' ));
add_action( 'created_' . self::TAXONOMY_NAME, array( __CLASS__, 'on_organization_added' ));
add_action( 'pre_delete_term', array( __CLASS__, 'on_taxonomy_term_will_be_deleted' ), 10, 2);
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 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
@@ -78,7 +89,7 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
if ($taxonomy === self::TAXONOMY_NAME) {
$user_id = $object_id;
$organization_id = $tt_ids[0];
add_user_meta($user_id, 'organization_id', $organization_id, true);
self::_add_user_to_access_group($user_id, $organization_id);
}
}
@@ -95,7 +106,7 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
if ($taxonomy === self::TAXONOMY_NAME) {
$user_id = $object_id;
$organization_id = $tt_ids[0];
delete_user_meta($user_id, 'organization_id');
self::_remove_user_from_organization_access_groups($user_id, $organization_id);
}
}
@@ -106,13 +117,13 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
* @param null $user_id
* @return mixed
*/
public static function get_user_organization($user_id = null) {
public static function get_user_organization_id($user_id = null) {
if (!isset($user_id)) {
$user = wp_get_current_user();
$user_id = $user->ID;
$user_id = get_current_user_id();
}
$terms = wp_get_object_terms($user_id, self::TAXONOMY_NAME);
return $terms[0];
$organization_id = get_user_meta($user_id, '_wiaas_organization_id', true);
return empty($organization_id) ? null : (int) $organization_id;
}
/**
@@ -124,8 +135,8 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
* @param $user_id
*/
public static function assign_post_to_user_organization($post_id, $user_id) {
$organization = self::get_user_organization($user_id);
self::_assign_post_to_organization($post_id, $organization->term_id);
$organization_id = self::get_user_organization_id($user_id);
self::_assign_post_to_organization($post_id, $organization_id);
}
@@ -240,73 +251,4 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
}
}
}
/**
* Show organizations selection on new user form
*/
function show_organizations_selection() {
$terms = get_terms( array(
'taxonomy' => self::TAXONOMY_NAME,
'hide_empty' => false,
) );
$taxonomy = get_taxonomy(self::TAXONOMY_NAME);
$this->table_contents(null, $taxonomy, $terms);
}
function edit_user_relationships($user = false) {
$tax = get_taxonomy( $this->taxonomy );
// Get the terms of the taxonomy.
$terms = get_terms( $this->taxonomy, array(
'hide_empty' => false
) );
$this->table_contents( $user, $tax, $terms );
}
function table_contents( $user, $tax, $terms ) {
$active_organization_id = -1;
if ($user) {
$active_organization = self::get_user_organization($user->ID);
$active_organization_id = $active_organization ? $active_organization->term_id : -1;
}
?>
<table class="form-table">
<tbody>
<tr class="form-field">
<th scope="row">
<label for="<?php echo esc_attr( $this->taxonomy ); ?>[]">
<?php echo esc_html( $tax->labels->singular_name ); ?>
</label>
</th>
<td>
<select name="<?php echo esc_attr( $this->taxonomy ); ?>[]" id="<?php echo esc_attr( $this->taxonomy ); ?>">
<?php
foreach ( $terms as $term ) :
$selected = $active_organization_id === $term->term_id;
?>
<option
value="<?php echo esc_attr( $term->slug ); ?>"
<?php selected( $selected ); ?>
>
<?php echo esc_attr( $term->name ); ?>
</option>
<?php
endforeach;
?>
</select>
<?php
wp_nonce_field( $this->taxonomy, $this->get_nonce_key() );
?>
</td>
</tr>
</tbody>
</table>
<?php
}
private function get_nonce_key() {
return "wp_user_taxonomy_{$this->taxonomy}";
}
}

View File

@@ -0,0 +1,105 @@
<?php
/**
* Retrieves name for provided organization_id
* @param int $organization_id
*
* @return string|null
*/
function wiaas_get_organization_name($organization_id) {
$term = get_term($organization_id);
if ($term instanceof WP_Term) {
return $term->name;
}
return null;
}
/**
* Retrieve array of organization role names
*
* @param int $organization_id
*
* @return array Array of organization roles
*/
function wiaas_get_organization_roles($organization_id) {
$roles = get_term_meta($organization_id, '_wiaas_organization_roles', true);
return empty($roles) ? array() : $roles;
}
/**
* @param int $organization_id
* @param array $roles
*/
function wiaas_set_organization_roles($organization_id, $roles) {
update_term_meta($organization_id, '_wiaas_organization_roles', $roles);
}
/**
* Retrieves list of organizations with provided role in [ 'id' => 'name'] format
* @param string $role
*
* @return array
*/
function wiaas_get_organizations_with_role($role) {
$terms = get_terms(array(
'taxonomy' => Wiaas_User_Organization::TAXONOMY_NAME,
'meta_key' => '_wiaas_organization_roles',
'meta_value' => $role,
'fields' => 'id=>name',
'meta_compare' => 'LIKE',
));
return is_array($terms) ? $terms : array();
}
/**
* Retrieves list of commercial lead organizations in [ 'id' => 'name' ] format
*
* @return array
*/
function wiaas_get_commercial_leads() {
return wiaas_get_organizations_with_role('shop_manager');
}
/**
* Retrieves list of customer organizations in [ 'id' => 'name'] format
*
* @return array
*/
function wiaas_get_customers() {
return wiaas_get_organizations_with_role('customer');
}
/**
* Retrieves list of supplier organizations in [ 'id' => 'name'] format
*
* @return array
*/
function wiaas_get_suppliers() {
return wiaas_get_organizations_with_role('supplier');
}
/**
* Retrieves id of organization for provided user_id
*
* @param int $user_id
*
* @return int|null
*/
function wiaas_get_user_organization_id($user_id) {
$organization_id = get_user_meta($user_id, '_wiaas_organization_id', true);
return empty($organization_id) ? null : (int) $organization_id;
}
/**
* Retrieves id of current user organization
*
* @return int|null
*/
function wiaas_get_current_user_organization_id() {
return wiaas_get_user_organization_id(get_current_user_id());
}

View File

@@ -37,7 +37,6 @@ function load_active_plugins_list() {
'jwt-authentication-for-wp-rest-api/jwt-auth.php',
'gravityforms/gravityforms.php',
'gravityflow/gravityflow.php',
'capability-manager-enhanced/capsman-enhanced.php',
'groups/groups.php',
'wp-user-groups/wp-user-groups.php',
'wiaas/wiaas.php'
@@ -65,5 +64,5 @@ function load_wiaas_test_setup() {
# Require classes needed for db updates
require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/includes/class-wiaas-db-update.php';
require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/includes/db-updates/wiaas-db-update-functions.php';
require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/includes/db-updates/wiaas-db-update-general.php';
}

View File

@@ -0,0 +1,388 @@
<?php
class Wiaas_Authentication_Test extends Wiaas_Unit_Test_Case {
var $user_id, $organization_id, $request_uri;
public function setUp() {
parent::setUp();
// set admin as current user
wp_set_current_user(1);
// create testing user
$this->user_id = wp_create_user('test', 'test', 'test@mail.com');
// create organization
$this->organization_id = wp_insert_term(
'test_organization',
Wiaas_User_Organization::TAXONOMY_NAME
)['term_id'];
update_user_meta($this->user_id, '_wiaas_organization_id', $this->organization_id);
# assign user to organization
wp_set_terms_for_user(
$this->user_id,
Wiaas_User_Organization::TAXONOMY_NAME,
[$this->organization_id]);
wp_set_current_user($this->user_id);
$this->request_uri = $_SERVER['REQUEST_URI'];
}
function tearDown() {
parent::tearDown();
wp_set_current_user(1);
wp_delete_user($this->user_id);
wp_delete_term(
$this->organization_id,
Wiaas_User_Organization::TAXONOMY_NAME);
delete_user_meta($this->user_id, '_wiaas_organization_id');
delete_user_meta($this->user_id, '_wiaas_current_user_admin_role');
$_SERVER['REQUEST_URI'] = $this->request_uri;
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_user_authentication_fail_when_no_selected_role() {
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
$this->assertTrue(
is_wp_error(
Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user())
)
);
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_user_authentication_forwards_error() {
// add roles to organization
$organization_roles = array( 'supplier', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', 'supplier');
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user(false)
);
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_fail_when_organization_has_no_roles() {
$roles = array('administrator', 'supplier', 'customer', 'commercial_lead');
foreach ($roles as $role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $role);
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_fail_when_organization_has_different_roles() {
// add roles to organization
$organization_roles = array( 'supplier', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = array('administrator', 'commercial_lead');
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_valid_when_organization_has_requested_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertEquals(
$this->user_id,
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_invalid_when_organization_has_no_customer_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_valid_when_organization_has_customer_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertEquals(
$this->user_id,
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_if_no_role_posted() {
$user = wp_get_current_user();
$error = Wiaas_Authentication::authenticate_user_on_login($user);
$this->assertTrue(is_wp_error($error));
$this->assertEquals('You must selected role to login!', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_does_nothing_if_rest_request() {
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
$user = wp_get_current_user();
$response_user = Wiaas_Authentication::authenticate_user_on_login($user);
$this->assertEquals(
$user->ID,
$response_user->ID
);
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_if_customer_role_requested() {
$user = wp_get_current_user();
$_POST['role'] = 'customer';
$error = Wiaas_Authentication::authenticate_user_on_login($user);
$this->assertTrue(is_wp_error($error));
$this->assertEquals('No access!', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_when_user_has_no_organization() {
$_POST['role'] = 'supplier';
delete_user_meta($this->user_id, '_wiaas_organization_id');
$error = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertTrue(is_wp_error($error));
$this->assertEquals('Account not completed!', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_when_organization_has_no_roles() {
$_POST['role'] = 'supplier';
$error = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertTrue(is_wp_error($error));
$this->assertEquals('Your account is not authorized for requested role. Please contact us for help.', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_when_organization_has_different_roles() {
// add roles to organization
$organization_roles = array( 'supplier', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$_POST['role'] = 'commercial_lead';
$error = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertTrue(is_wp_error($error));
$this->assertEquals('Your account is not authorized for requested role. Please contact us for help.', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_valid_when_organization_has_requested_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
foreach ($user_roles as $user_role) {
$_POST['role'] = $user_role;
$response_user = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertEquals(
$this->user_id,
$response_user->ID
);
}
}
/**
* @covers Wiaas_Authentication::maybe_filter_user_roles()
* @group authentication
*/
function test_user_has_customer_role_on_rest_request() {
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
global $wpdb;
$user_roles = Wiaas_Authentication::maybe_filter_user_roles(
null, $this->user_id,
$wpdb->get_blog_prefix() . 'capabilities'
);
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$user_roles = $user_roles[0];
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$this->assertArrayHasKey('customer', $user_roles);
$this->assertTrue($user_roles['customer']);
}
/**
* @covers Wiaas_Authentication::maybe_filter_user_roles()
* @group authentication
*/
function test_user_has_no_role_if_not_selected() {
global $wpdb;
$user_roles = Wiaas_Authentication::maybe_filter_user_roles(
null, $this->user_id,
$wpdb->get_blog_prefix() . 'capabilities'
);
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$user_roles = $user_roles[0];
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$this->assertEmpty(array_keys($user_roles)[0]);
}
/**
* @covers Wiaas_Authentication::maybe_filter_user_roles()
* @group authentication
*/
function test_user_has_selected_role() {
global $wpdb;
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', 'supplier');
$user_roles = Wiaas_Authentication::maybe_filter_user_roles(
null, $this->user_id,
$wpdb->get_blog_prefix() . 'capabilities'
);
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$user_roles = $user_roles[0];
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$this->assertArrayHasKey('supplier', $user_roles);
$this->assertTrue($user_roles['supplier']);
}
}

View File

@@ -28,6 +28,8 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case {
Wiaas_User_Organization::TAXONOMY_NAME
)['term_id'];
update_user_meta($this->customer_id, '_wiaas_organization_id', $this->customer_organization_id);
# add customer to organization
wp_set_terms_for_user(
$this->customer_id,
@@ -198,28 +200,4 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case {
$this->assertArrayHasKey('email', $transformed_order_response['commercial_lead']);
$this->assertArrayHasKey('phone', $transformed_order_response['commercial_lead']);
}
/**
* @covers Wiaas_Order::transform_rest_order()
*/
function test_order_rest_response_has_empty_documents_when_package_has_no_documents() {
$order_response = array(
'customer_id' => $this->customer_id,
'status' => 'processing',
'line_items' => array()
);
$request = array( 'id' => $this->order_id);
$order_rest_response = new WP_REST_Response($order_response);
$order_rest_response = Wiaas_Order::transform_rest_order(
$order_rest_response,
wc_get_order($this->order_id),
$request);
$transformed_order_response = $order_rest_response->get_data();
$this->assertNotNull($transformed_order_response['documents']);
$this->assertTrue(is_array($transformed_order_response['documents']));
}
}

View File

@@ -28,6 +28,8 @@ class Wiaas_User_Organization_Test extends Wiaas_Unit_Test_Case {
Wiaas_User_Organization::TAXONOMY_NAME
)['term_id'];
update_user_meta($this->user_id, '_wiaas_organization_id', $this->user_organization_id);
# create department
$this->user_department_id = wp_insert_term(
$this->user_department_name,
@@ -63,11 +65,10 @@ class Wiaas_User_Organization_Test extends Wiaas_Unit_Test_Case {
* @covers Wiaas_User_Organization::get_user_organization()
*/
function test_retrieve_user_organization() {
$organization = Wiaas_User_Organization::get_user_organization($this->user_id);
$organization_id = Wiaas_User_Organization::get_user_organization_id($this->user_id);
$this->assertNotNull($organization);
$this->assertEquals($organization->term_id, $this->user_organization_id);
$this->assertEquals($organization->name, $this->user_organization_name);
$this->assertNotNull($organization_id);
$this->assertEquals($organization_id, $this->user_organization_id);
}
/**

View File

@@ -26,6 +26,8 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase {
Wiaas_Package_Type::register_package_type_taxonomy();
Wiaas_Order_Project::register_order_project_taxonomy();
Wiaas_Product_Supplier::register_supplier_taxonomy();
define('WP_TEST_IN_PROGRESS',true);
}

View File

@@ -22,6 +22,13 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once WIAAS_DIR . '/includes/class-wiaas-cli.php';
}
include_once WIAAS_DIR . '/includes/class-wiaas-authentication.php';
if (is_admin()) {
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';
}
include_once WIAAS_DIR . '/includes/class-wiaas-delivery-process.php';
include_once WIAAS_DIR . '/includes/class-wiaas-order.php';
@@ -44,6 +51,10 @@ include_once WIAAS_DIR . '/includes/class-wiaas-checkout.php';
include_once WIAAS_DIR . '/includes/class-wiaas-api.php';
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';
include_once WIAAS_DIR . '/includes/class-wiass-templates.php';
function wiaas_redirect_to_login() {
wp_safe_redirect(get_site_url('', 'wp-login.php'));
}
add_filter('wp_die_handler', 'wiaas_redirect_to_login');

View File

@@ -62,6 +62,8 @@
"wpackagist-plugin/capability-manager-enhanced": "1.5.9",
"wpackagist-plugin/wp-user-groups": "2.2.0",
"wpackagist-plugin/radio-buttons-for-taxonomies": "1.8.3",
"wpackagist-plugin/advanced-access-manager": "5.4.3.2",
"wpackagist-plugin/advanced-custom-fields" : "5.7.7",
"3rdparty/gravityforms": "*",
"3rdparty/gravityflow": "*",
@@ -96,6 +98,8 @@
"wp plugin activate groups",
"wp plugin activate wp-user-groups",
"wp plugin activate radio-buttons-for-taxonomies",
"wp plugin activate advanced-access-manager",
"wp plugin activate advanced-custom-fields",
"wp plugin activate wiaas"
],
"update-db": [

42
backend/composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "d979d3435513dd818189e2f68cc023fe",
"content-hash": "da3d7fa874b9a31355d3c3d4df50ae94",
"packages": [
{
"name": "3rdparty/gravityflow",
@@ -428,6 +428,46 @@
"homepage": "https://woocommerce.com/",
"time": "2018-07-25T18:00:37+00:00"
},
{
"name": "wpackagist-plugin/advanced-access-manager",
"version": "5.4.3.2",
"source": {
"type": "svn",
"url": "https://plugins.svn.wordpress.org/advanced-access-manager/",
"reference": "tags/5.4.3.2"
},
"dist": {
"type": "zip",
"url": "https://downloads.wordpress.org/plugin/advanced-access-manager.5.4.3.2.zip",
"reference": null,
"shasum": null
},
"require": {
"composer/installers": "~1.0"
},
"type": "wordpress-plugin",
"homepage": "https://wordpress.org/plugins/advanced-access-manager/"
},
{
"name": "wpackagist-plugin/advanced-custom-fields",
"version": "5.7.7",
"source": {
"type": "svn",
"url": "https://plugins.svn.wordpress.org/advanced-custom-fields/",
"reference": "tags/5.7.7"
},
"dist": {
"type": "zip",
"url": "https://downloads.wordpress.org/plugin/advanced-custom-fields.5.7.7.zip",
"reference": null,
"shasum": null
},
"require": {
"composer/installers": "~1.0"
},
"type": "wordpress-plugin",
"homepage": "https://wordpress.org/plugins/advanced-custom-fields/"
},
{
"name": "wpackagist-plugin/akismet",
"version": "4.0.3",

View File

@@ -63,6 +63,10 @@ define('NONCE_SALT', env('WP_NONCE_SALT'));
define('JWT_AUTH_SECRET_KEY', env('WP_JWT_AUTH_SECRET_KEY'));
define('JWT_AUTH_CORS_ENABLE', true);
/**
* Wiaas Env variables
*/
define('WIAAS_CUSTOMER_INTERFACE', env('WIAAS_CUSTOMER_INTERFACE') ?: WP_HOME);
/**
* Custom Settings

View File

@@ -7,6 +7,7 @@ services:
dockerfile: backend.dockerfile
args:
- API_URL
- WIAAS_CUSTOMER_INTERFACE
- MYSQL_DATABASE
- MYSQL_USER
- MYSQL_PASSWORD

View File

@@ -7,6 +7,8 @@ MYSQL_PASSWORD=wp_password
# Url where backend application api is available
API_URL=http://localhost
WIAAS_CUSTOMER_INTERFACE=http://localhost:8080
#Wordpress config
# Determines loading of enviroment specific non sensitive wordpress config file (/backend/config/environments/[WP_ENV].php)