How to get list of permissions in Yii2? - php

I need somehow to get the list of existing permissions in my Yii2 application.
yii/rbac/DbManager as authManager is used.
And when trying to run:
$permissions = Yii::$app->authManager->getPermissions();
I receive list in which are permissions and routes:
Array ( [/admin-tools/*] => yii\rbac\Permission Object ( [type] => 2 [name] => /admin-tools/* [description] => Access admin-tools [ruleName] => [data] => [createdAt] => 1500542371 [updatedAt] => 1500542371 ) [/admin/*] => yii\rbac\Permission Object ( [type] => 2 [name] => /admin/* [description] => access admin extension [ruleName] => [data] => [createdAt] => 1500541632 [updatedAt] => 1500541632 ) [/group] => yii\rbac\Permission Object ( [type] => 2 [name] => /group [description] => [ruleName] => [data] => [createdAt] => 1502190828 [updatedAt] => 1502190828 ) ) [permission_admin] => yii\rbac\Permission Object ( [type] => 2 [name] => permission_admin [description] => [ruleName] => [data] => [createdAt] => 1500559417 [updatedAt] => 1502190927 ) [permission_hr] => yii\rbac\Permission Object ( [type] => 2 [name] => permission_hr [description] => [ruleName] => [data] => [createdAt] => 1500559479 [updatedAt] => 1500559919 ) )
I suppose there is another smarter way to get this data. Because yii2-admin module is able to get list of only permissions, but I don`t have any idea how to do this?.

The resulting
$permissions = Yii::$app->authManager->getPermissions();
is an array with all the permissions object content
if you need only name and description you can iterate over the array eg:
$permissions = Yii::$app->authManager->getPermissions();
foreach($permissions as $key => $perm){
echo 'name : ' . $perm->name . ' description : ' . $perm->description . <br />;
echo
}
you can check yii2 doc http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html
http://www.yiiframework.com/doc-2.0/yii-rbac-basemanager.html#getPermissions()-detail
could b you are looking for getPermissionsByUser()
http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html#getPermissionsByUser()-detail

Related

How i display the json array data from api [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
Below is my data get from API , I try to display the shopId, Shopname and shopcode but failed.Any one can help me solve this problem ? I am using the PHP to write the script and get API data.Below is my script to get API data.
<?php
$get = file_get_contents("http://api.meirenji.cn/api/xxxxxxxxxxx");
$json = json_decode($get);
$shop_id = $json->content->shopId;
echo "Shop ID : ".$shop_id."";
?>
Below is the data that i get from API, it have multiple array data, i nee get data shopId, Shopname,shopcode
stdClass Object
(
[status] => 0
[msg] => succes
[content] => Array
(
[0] => stdClass Object
(
[shopId] => 6674
[shopname] => SP
[shopcode] => 38862185
[type] => shop
[flag] => 1
)
[1] => stdClass Object
(
[shopId] => 6498
[shopname] => Adidas
[shopcode] => 74812597
[type] => shop
[flag] => 1
)
[2] => stdClass Object
(
[shopId] => 6498
[shopname] => Nike
[shopcode] => 98741852
[type] => shop
[flag] => 1
)
[3] => stdClass Object
(
[shopId] => 6501
[shopname] => Puma
[shopcode] => 13847915
[type] => shop
[flag] => 1
)
[4] => stdClass Object
(
[shopId] => 6509
[shopname] => Adidas NEO
[shopcode] => 26700485
[type] => shop
[flag] => 1
)
[5] => stdClass Object
(
[shopId] => 6865
[shopname] => Testing Unit
[shopcode] => 23891935
[type] => shop
[flag] => 1
)
[6] => stdClass Object
(
[shopId] => 9661
[shopname] => UNDER ARMOR
[shopcode] => 88741294
[type] => shop
[flag] => 1
)
)
)
To get shopname and shopcode from your response object, you've to loop over your content like this way using foreach(),
foreach($json->content as $key=>$row){
echo "shopname = $row->shopname and shopcode = $row->shopcode". PHP_EOL;
}
DEMO: https://3v4l.org/tWqt7

Access created_on from response object - get file action PODIO API

Hello folks I'm trying to access the created_on object using code below but it is not working, I'm getting incorrect date January 1, 1970 12:00. Also tried
$item = PodioItem::get($item_id);
$file_id = "";
$created_on = "";
foreach ($item->files as $file) {
echo date('F j, Y h:i',strtotime($file->created_on))."<br>";
}
Below is the response object from print_r($file,true), I can access the other object without any issue like $file->file_id, $file->link. What I'm trying to accomplish is store the created_on data in a variable then create a function to check and grab the latest date, then grab the file_id of it.
PodioFile Object
[__attributes:PodioObject:private] => Array
(
[file_id] => 464714088
[link] => https://files.podio.com/464714088
[perma_link] =>
[thumbnail_link] =>
[hosted_by] => podio
[name] => KEjj4UQSUV.PDF
[description] =>
[mimetype] => application/pdf
[size] => 785849
[created_on] => 2017-12-08 23:03:36
[rights] => Array
(
[0] => download
[1] => view
[2] => delete
[3] => update
)
PodioFile Object
(
[__attributes:PodioObject:private] => Array
(
[file_id] => 464716575
[link] => https://files.podio.com/464716575
[perma_link] =>
[thumbnail_link] =>
[hosted_by] => podio
[name] => IiaCfJjwws.PDF
[description] =>
[mimetype] => application/pdf
[size] => 785849
[created_on] => 2017-12-08 23:15:30
[rights] => Array
(
[0] => download
[1] => view
[2] => delete
[3] => update
)
PodioFile Object
(
[__attributes:PodioObject:private] => Array
(
[file_id] => 464728331
[link] => https://files.podio.com/464728331
[perma_link] =>
[thumbnail_link] =>
[hosted_by] => podio
[name] => g6cmAeUCiV.PDF
[description] =>
[mimetype] => application/pdf
[size] => 785849
[created_on] => 2017-12-08 23:49:15
[rights] => Array
(
[0] => download
[1] => view
[2] => delete
[3] => update
)
Luckily found the answer on sample provided here - https://hotexamples.com/examples/-/PodioItem/filter/php-podioitem-filter-method-examples.html
date_format($file->created_on, 'Y/m/d H:i:s') did the trick

Server based Google Drive Service?

I am looking to build a Google Drive application to store image and document files from a website application, without the requirement of a visitor authenticating, or providing permission to access, etc. Seamless use of a googledrive service through a PHP interface.
Users will upload images/documents to a web form, and the files are stored on a GoogleDrive instead of local file system.
I'm just in need of assistance with this, as all the Google Client examples I've worked with require user based authorization, and permissions.
The code I have now seems to get me my Google_DriveService array:
require_once '../apis/google-api-php-client/src/Google_Client.php';
require_once '../apis/google-api-php-client/src/contrib/Google_PredictionService.php';
require '../apis/google-api-php-client/src/contrib/Google_DriveService.php';
session_start();
// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = '########.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '##########developer.gserviceaccount.com';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '##########-privatekey.p12';
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),$key));
$client->setClientId(CLIENT_ID);
$service = new Google_PredictionService($client);
// Prediction logic:
$id = '9146497114232; # replaced with random numbers for this post
$hostedModelName= 'sample.languageid';
$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array('Foo Bar A Doo')); ## Not sure what this is or what I need here?
$input = new Google_Input();
$input->setInput($predictionData);
$result = $service->hostedmodels->predict($id,$hostedModelName, $input);
# print '<h2>Prediction Result:</h2><pre>' . print_r($result, true) . '</pre>';
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
$driveService = new Google_DriveService($client);
print_r($driveService);
exit;
I am returned a lengthy array of objects, and from there, I'm rather stone-walled.
Some of which looks like :
[service:Google_ServiceResource:private] => Google_DriveService Object
*RECURSION*
[serviceName:Google_ServiceResource:private] => drive
[resourceName:Google_ServiceResource:private] => revisions
[methods:Google_ServiceResource:private] => Array
(
[delete] => Array
(
[id] => drive.revisions.delete
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => DELETE
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
)
)
[get] => Array
(
[id] => drive.revisions.get
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => GET
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[response] => Array
(
[$ref] => Revision
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
[2] => https://www.googleapis.com/auth/drive.metadata.readonly
[3] => https://www.googleapis.com/auth/drive.readonly
)
)
[list] => Array
(
[id] => drive.revisions.list
[path] => files/{fileId}/revisions
[httpMethod] => GET
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[response] => Array
(
[$ref] => RevisionList
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
[2] => https://www.googleapis.com/auth/drive.metadata.readonly
[3] => https://www.googleapis.com/auth/drive.readonly
)
)
[patch] => Array
(
[id] => drive.revisions.patch
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => PATCH
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[request] => Array
(
[$ref] => Revision
)
[response] => Array
(
[$ref] => Revision
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
)
)
[update] => Array
(
[id] => drive.revisions.update
[path] => files/{fileId}/revisions/{revisionId}
[httpMethod] => PUT
[parameters] => Array
(
[fileId] => Array
(
[type] => string
[required] => 1
[location] => path
)
[revisionId] => Array
(
[type] => string
[required] => 1
[location] => path
)
)
[request] => Array
(
[$ref] => Revision
)
[response] => Array
(
[$ref] => Revision
)
[scopes] => Array
(
[0] => https://www.googleapis.com/auth/drive
[1] => https://www.googleapis.com/auth/drive.file
)
)
)
)
[version] => v2
[servicePath] => drive/v2/
[resource] =>
[serviceName] => drive
I think you should look into using a service acccount for this.
https://developers.google.com/drive/web/service-accounts
By using a service account you wont have to worry about people loging in. Everthing will always be uploaded to the same service account.

requesting user permissions not working facebook graph api

hi i am using facebook graph api PHP SDK . here i am trying to get the logged in user's facebook details , currently i am getting only the basic details which are not required any permissions .
Array
(
[id] => xxxxxxxxx
[name] => Kanishka Bandara Panamaldeniya
[first_name] => Kanishka
[middle_name] => Bandara
[last_name] => Panamaldeniya
[link] => http://www.facebook.com/kanishka.panamaldeniya
[username] => kanishka.panamaldeniya
[hometown] => Array
(
[id] => 108014839219027
[name] => Kandy
)
[location] => Array
(
[id] => 108602292505393
[name] => Colombo, Sri Lanka
)
[quotes] => every action has a reaction....
[work] => Array
(
[0] => Array
(
[employer] => Array
(
[id] => 234117399957729
[name] => Cyber LMJ
)
[location] => Array
(
[id] => 108602292505393
[name] => Colombo, Sri Lanka
)
[position] => Array
(
[id] => 109542932398298
[name] => Software Engineer
)
[start_date] => 2011-08
)
[1] => Array
(
[employer] => Array
(
[id] => 235852306375
[name] => eMarketingEye
)
[position] => Array
(
[id] => 109542932398298
[name] => Software Engineer
)
[start_date] => 2010-09
[end_date] => 2011-08
)
)
[sports] => Array
(
[0] => Array
(
[id] => 103992339636529
[name] => Cricket
)
)
[favorite_teams] => Array
(
[0] => Array
(
[id] => 191485640872790
[name] => Sri Lanka Cricket Team
)
)
[favorite_athletes] => Array
(
[0] => Array
(
[id] => 185101794860886
[name] => Kumara Sangakkara
)
[1] => Array
(
[id] => 119473804782315
[name] => Angelo Mathews
)
[2] => Array
(
[id] => 164574093575397
[name] => Kumar Sangakkara
)
[3] => Array
(
[id] => 169411436417102
[name] => Virat Kohli
)
[4] => Array
(
[id] => 166273026737204
[name] => Lahiru Thirimanne
)
[5] => Array
(
[id] => 101237886616765
[name] => Dinesh Chandimal
)
)
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 34663330581
[name] => Dharmaraja College-Kandy
)
[year] => Array
(
[id] => 138383069535219
[name] => 2005
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 112872282058324
[name] => University of Colombo School of Computing
)
[year] => Array
(
[id] => 140617569303679
[name] => 2007
)
[type] => College
)
[2] => Array
(
[school] => Array
(
[id] => 107577089271805
[name] => University of Colombo
)
[type] => Graduate School
[with] => Array
(
[0] => Array
(
[id] => 705760115
[name] => Lasitha Benaragama
)
)
)
)
[gender] => male
[timezone] => 5.5
[locale] => en_US
[languages] => Array
(
[0] => Array
(
[id] => 104023029632921
[name] => Sinhala
)
[1] => Array
(
[id] => 106059522759137
[name] => English
)
)
[verified] => 1
[updated_time] => 2012-07-29T16:34:22+0000
)
now i want to get fields
*email
*birth day
and some other permission requesting fields .
so i am doing
$this->co_config_items = $this->ci->config->item('connections_config');
$fbconfig = array(
'appId' => $this->co_config_items['fb_appid'],
'secret' => $this->co_config_items['fb_secret'],
'cookie' => true,
'domain' => $this->ci->config->item('domain')
);
$this->ci->load->library('3rd/connectors/facebook', $fbconfig);
$facebook = new Facebook($fbconfig);
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$session = $facebook->getSession();
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
));
if(!$session){
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
));
echo "<script type='text/javascript'>window.location = '$url';</script>";
}
else{
//echo "<script type='text/javascript'>window.opener.location.href='" . base_url() . "home/auth/get_user_facebook_details_list" . "'; self.close();
//</script>";
try {
$me = $facebook->api('/me');
$this->ci->session->set_flashdata ( 'user_details',$me);
$url=base_url()."home/auth/user_contact_information";
echo "<script type='text/javascript'>window.opener.location.href = '$url';self.close();</script>";
} catch (FacebookApiException $e) {
$this->ci->message->set_error(array('An errror occured , please try again'));
}
}
i am trying to ask permissions by
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
)):
but it is not asking for any permission , the default array is always printing . why is that . please help . thanks in advance
Try doing this instead:
$url = $facebook->getLoginUrl(array('scope' => 'read_stream,friends_likes,user_birthday'));
i was using the version 2 of PHP sdk , now i am using version 3 , now it is working , thank you very much . i saw some changes in two versions in this function
version 2
public function getLoginUrl($params=array()) {
$currentUrl = $this->getCurrentUrl();
return $this->getUrl(
'www',
'login.php',
array_merge(array(
'api_key' => $this->getAppId(),
'cancel_url' => $currentUrl,
'display' => 'page',
'fbconnect' => 1,
'next' => $currentUrl,
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',
), $params)
);
}
version 3
public function getLoginUrl($params=array()) {
$this->establishCSRFTokenState();
$currentUrl = $this->getCurrentUrl();
// if 'scope' is passed as an array, convert to comma separated list
$scopeParams = isset($params['scope']) ? $params['scope'] : null;
if ($scopeParams && is_array($scopeParams)) {
$params['scope'] = implode(',', $scopeParams);
}
return $this->getUrl(
'www',
'dialog/oauth',
array_merge(array(
'client_id' => $this->getAppId(),
'redirect_uri' => $currentUrl, // possibly overwritten
'state' => $this->state),
$params));
}
in version 3 this i can see this additional code
// if 'scope' is passed as an array, convert to comma separated list
$scopeParams = isset($params['scope']) ? $params['scope'] : null;
if ($scopeParams && is_array($scopeParams)) {
$params['scope'] = implode(',', $scopeParams);
}
in version 2 , there is nothing called scope .and also
in version 2
return $this->getUrl(
'www',
'login.php',
in version 3
return $this->getUrl(
'www',
'dialog/oauth',

Stackoverflow API getting answers in array PHP

I'm trying to use the stackoverflow API and I want to get answers of a question in a php array. So far here is my php code:
<?php
//KEY
$string = "key=my_key";
//Call stack API .$string
$stack_url = "compress.zlib://http://api.stackoverflow.com/1.1/questions";
//Get and Store API results into a variable
$result = file_get_contents($stack_url);
$jsonArray = json_decode($result);
print_r($jsonArray);
//echo($jsonArray->questions[0]->question_answers_url);
//var_dump($jsonArray);
?>
I want to store the answers of a question in an array called answers so that I can access them with a for loop.
The answer i get is :
stdClass Object
(
[total] => 2618591
[page] => 1
[pagesize] => 30
[questions] => Array
(
[0] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => ssh
[2] => openssh
[3] => rsacryptoserviceprovider
)
[answer_count] => 1
[favorite_count] => 0
[question_timeline_url] => /questions/9164203/timeline
[question_comments_url] => /questions/9164203/comments
[question_answers_url] => /questions/9164203/answers
[question_id] => 9164203
[owner] => stdClass Object
(
[user_id] => 311966
[user_type] => registered
[display_name] => simonc
[reputation] => 301
[email_hash] => 021f3344004f0c886d715314fa02037d
)
[creation_date] => 1328548627
[last_edit_date] => 1328611688
[last_activity_date] => 1328611688
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 25
[score] => 0
[community_owned] =>
[title] => Format of PKCS private keys
)
[1] => stdClass Object
(
[tags] => Array
(
[0] => c#
[1] => .net
[2] => combobox
)
[answer_count] => 3
[favorite_count] => 0
[question_timeline_url] => /questions/9174765/timeline
[question_comments_url] => /questions/9174765/comments
[question_answers_url] => /questions/9174765/answers
[question_id] => 9174765
[owner] => stdClass Object
(
[user_id] => 1194399
[user_type] => registered
[display_name] => Goxy
[reputation] => 1
[email_hash] => 5fc8c96b6b85c6339cb9ac4ab60cb247
)
[creation_date] => 1328611202
[last_activity_date] => 1328611686
[up_vote_count] => 0
[down_vote_count] => 0
[view_count] => 15
[score] => 0
[community_owned] =>
[title] => WPF: Bind simple List<myClass> to Combobox
)
....
Not sure exactly which property you want to extract, but I assume it's the 'question_answers_url'.
$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
//assuming it is the 'question_answers_url' property that you want
array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}
Ought to do it.

Categories