PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label user-roles. Show all posts
Showing posts with label user-roles. Show all posts

Friday, June 24, 2022

[FIXED] How to map apache authgroupfile with varnish-cache

 June 24, 2022     reverse-proxy, user-roles, varnish     No comments   

Issue

Hi Friends,

Please help, we are trying add user and roles to our legacy application by mapping users in Apache AuthgroupFile with varnish-cache reverse-proxy, any user authenticated through Apache Basic Auth should be able to go through; The user is mapped to the role in the AuthgroupFile and in back-end we check for the group name and assign the role in the application

can we read the AuthgroupFile to a variable and in varnish-cache and check for the REMOTE_USER header?

#AuthgroupFile admin: foo boo roo readonly: goo too zoo

#varnish-cache rule

if (req.http.REMOTE_USER){
   set req.http.X-AUTH-USER = req.http.REMOTE_USER;
}

Solution

If you want to check for authenticated users, I'd advise you to have a look at vmod_basicauth.

Its a Varnish module that reads an .htpasswd file and gives you a VCL API to interact with these logins.

Here's how to use this module in VCL:

if (!basicauth.match("/var/www/.htpasswd", req.http.Authorization)) {
    return(synth(401, "Restricted"));
}

This will match the Authorization request header to whatever is allowed in .htpasswd.

Is this what you're looking for?



Answered By - Thijs Feryn
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, March 13, 2022

[FIXED] Show only if user-role is subscriber AND site visitor (Wordpress)

 March 13, 2022     adsense, user-roles, wordpress     No comments   

Issue

I am a bit stuck with the code below for my Wordpress site:

<?php
    $user = wp_get_current_user();
    $allowed_roles = array('subscriber', 'visitor');
     if ( array_intersect($allowed_roles, $user->roles ) ) echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXX" crossorigin="anonymous"></script>' ?>

I want the Adsense code only be shown in the HEAD section for site visitors and the user role SUBSCRIBER. I tried 'visitor' but that didn't worked.

I don't know how to apply "array_diff" in this situation to display the Adsense code in the header for SITE VISITORS and SUBSCRIBER role?


Solution

For "subscriber" you can use function current_user_can

For guests you can use function is_user_logged_in()

if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
           // echo your adsense code
}


Answered By - Valerii Vasyliev
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, January 17, 2022

[FIXED] Add extra customer note on order creation for specific user role in WooCommerce

 January 17, 2022     backend, orders, user-roles, woocommerce, wordpress     No comments   

Issue

For a specific user role I want to add a specific customer note to the order.

This is my code:

add_action( 'woocommerce_new_order', 'add_customer_note_user_role' );
function add_customer_note_user_role( $order_id ) {

    $user_info = get_userdata(get_current_user_id());

    if ( $user_info->roles[0]=="administrator" ) {

        $order = wc_get_order( $order_id );

        // The text for the note
        $note = 'This is the message';

        // Add the note
        $order->add_order_note( $note );

        // Save the data
        $order->save();
    }
}

But this puts the message in the wrong place. When you check an order in the backend it's been displayed in the purple message boxes.

I want the message to be displayed as a customer note which is displayed under the shipping address. Because my API is picking up the notes from that place and puts them in our ERP.

I tried to change

$order->add_order_note( $note );

to

$order->add_order_note( $note, 'is_customer_note', true );

But without the desired result, any advice?


Solution

To display the message as a customer note displayed below the shipping address, you can use set_customer_note() instead.

So you get:

function action_woocommerce_new_order( $order_id ) {
    // Get the WC_Order Object
    $order = wc_get_order( $order_id );

    // Get the WP_User Object
    $user = $order->get_user();
    
    // Check for "administrator" user roles only
    if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', (array) $user->roles ) ) {
        // The text for the note
        $note = 'This is the message';
        
        // Set note
        $order->set_customer_note( $note );
        
        // Save
        $order->save();
    }
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );

To keep the original message (when NOT empty) use this instead:

function action_woocommerce_new_order( $order_id ) {
    // Get the WC_Order Object
    $order = wc_get_order( $order_id );

    // Get the WP_User Object
    $user = $order->get_user();
    
    // Check for "administrator" user roles only
    if ( is_a( $user, 'WP_User' ) && in_array( 'administrator', (array) $user->roles ) ) {
        // The text for the note
        $note = 'This is the message';
        
        // Get customer note
        $customer_note = $order->get_customer_note();
        
        // NOT empty
        if ( ! empty ( $customer_note ) ) {
            $note = $customer_note . ' | ' . $note;
        }
        
        // Set note
        $order->set_customer_note( $note );
        
        // Save
        $order->save();
    }
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );


Answered By - 7uc1f3r
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing