Kit Sunde
Kit Sunde

Reputation: 37095

How can I check if a user is an admin of a page without the manage_pages permission?

Is that possible? I don't actually want to manage their pages, I just want to know if they are an admin of the page so we can grant the user special permission in the Facebook application.

Upvotes: 0

Views: 308

Answers (1)

ifaour
ifaour

Reputation: 38135

Facebook will send that piece of info when the admin land on your Page Tab inside the signed_request (reference):

<?php
if(!empty($_REQUEST["signed_request"])) {
    $app_secret = "APP_SECRET";
    $data = parse_signed_request($_REQUEST["signed_request"], $app_secret);

    if (empty($data["page"]["admin"])) {
        echo "You are not an admin!";
    } else {
        echo "Welcome Admin!";
    }
}

function parse_signed_request($signed_request, $secret) {
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

    // decode the data
    $sig = base64_url_decode($encoded_sig);
    $data = json_decode(base64_url_decode($payload), true);

    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
        error_log('Unknown algorithm. Expected HMAC-SHA256');
        return null;
    }

    // check sig
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
        error_log('Bad Signed JSON signature!');
        return null;
    }

    return $data;
}

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));
}

?>

Upvotes: 1

Related Questions