Get userId on database in Silex Project - php

I started a new project with Silex, it looks like a cool and fast Framework but I'm getting crazy with the SecurityServiceProvider trying to get the uid. I though it should be something like the getId() like Symfony but I got an error that the function doesn't exists.
PHP Fatal error: Call to undefined method Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::getUser()
If I got the current logged user I have his information but not the Id.
$user = $app['user'];
print_r($user);
Symfony\Component\Security\Core\User\User Object (
[username:Symfony\Component\Security\Core\User\User:private] =>
myuser#mymail.com
[password:Symfony\Component\Security\Core\User\User:private] =>
5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==
[enabled:Symfony\Component\Security\Core\User\User:private] => 1
[accountNonExpired:Symfony\Component\Security\Core\User\User:private]
=> 1 [credentialsNonExpired:Symfony\Component\Security\Core\User\User:private]
=> 1 [accountNonLocked:Symfony\Component\Security\Core\User\User:private]
=> 1 [roles:Symfony\Component\Security\Core\User\User:private] => Array ( [0] => ROLE_USER ) )
Any idea? Thanks in advance!

// Symfony 2.6+
$token = $app['security.token_storage']->getToken();
// Symfony 2.3/2.5
$token = $app['security']->getToken();
Get user info:
$user = $token->getUser();
Check $user
var_dump($user);

Ok got the solution, if anybody implements a new UserProvider, should to add the customs fields on the provider, and define them on User class in Symfony. Thanks anyway!

Related

Path not found on PodioAppField::update

I am following the documentation https://developers.podio.com/doc/applications
and the given models from the SDK.
Im trying to add a new app to an existing app reference like so:
$attributes = array(
"referenced_apps" => array(ref_app_id1 , ref_app_id2)
);
PodioAppField::update( 123 , 'my_external_id' ,array (
"settings" => $attributes
));
But will receive the following error
Uncaught PodioNotFoundError: "No matching operation could be found. The path '/app/123/field/my_external_id' was not found.."
My app_id and external_id are correct.
TIA
It requires field_id instead of external_id.

WHMCS: How to get the current client in addon module clientarea page?

Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here

Moodle cohort_add_cohort error

I'm developing a custom Moodle authentification plugin for Moodle 2.7.
When a user is authenticated I want them to be added to a specific cohort. If that cohort does not exist I need it to be created automatically. I use the user_authenticated_hook() function in my authentification plugin to achieve this.
My code for creating the cohort is this
$data = new stdClass();
$data->name = 'Name string';
$data->idnumber = 'ID string';
$data->description = 'Description string';
$cohortId = cohort_add_cohort($data);
I have included cohort/lib.php in the auth.php file and I have declared the global variables $DB, $CFG and $SESSION at the first line of the user_authenticated_hook() function.
The authentification works without the part about cohorts. But with the cohort part in place authentication fails and I am redirected to the login page.
The page title is changed to "Error" but that is the only error message I get.
What Am I doing wrong? I hope somebody will be able to help me create cohorts and add members.
It might be because the global $USER object doesn't exist yet or hasn't been populated.
Do you have debug switched on in your config.php?
$CFG->debug = 32767;
$CFG->debugdisplay = 1;
It might be better to respond to the user_created event. So if a user is created by another method, they will still be added to the cohort. eg:
Create a local plugin eg:
/local/add_cohorts
Create an events.php
/local/add_cohorts/db/events.php
Which has something like this
$handlers = array (
'user_created' => array (
'handlerfile' => '/local/add_cohorts/lib.php',
'handlerfunction' => 'local_add_cohorts_user_created',
'schedule' => 'instant',
'internal' => 1,
),
);
Then in /local/add_cohorts/lib.php have
function local_add_cohorts_user_created($user) {
// Do your cohort processing here and add the user
// Use $user->id to add to the cohort members
}
Then create a version.php and install the plugin, then the event handler will be registered.
Events api - https://docs.moodle.org/dev/Events_API

Laravel 4 with Sentry 2 add user to a group on Registration

So, I'm trying to implement Sentry 2 with Laravel 4.1. I'm using this resource for it
https://github.com/rydurham/L4withSentry
Everything works fine, but now I want to assign user to a group automatically when he registers.
From Sentry 2 Docs,
// Find the group using the group id
$adminGroup = Sentry::findGroupById(5);
// Assign the group to the user
$user->addGroup($adminGroup);
Should work. So I added this lines of code to
Authority\Repo\User\SentryUser.php
Now the code looks like
try {
//Attempt to register the user.
$user = $this->sentry->register(array(
'username' => e($data['username']),
'email' => e($data['email']),
'password' => e($data['password'])
));
// Find the group using the group id
$adminGroup = Sentry::findGroupById(5);
// Assign the group to the user
$user->addGroup($adminGroup);
//success!
$result['success'] = true;
$result['message'] = trans('users.created');
$result['mailData']['activationCode'] = $user->GetActivationCode();
$result['mailData']['userId'] = $user->getId();
$result['mailData']['email'] = e($data['email']);
}
But this throws in an error
Non-static method Cartalyst\Sentry\Sentry::findGroupById() should not be called statically, assuming $this from incompatible context
Can anyone shed a light and tell me what I'm doing wrong?
Thanks in Advance
Somehow, where you are using Sentry, you're not using its Facade, but the class itself. When you call a class through a Facade you're not really using statics, it's just looks like you are.
Do you have this:
use Cartalyst\Sentry\Sentry;
In your code?
Ok, but if this line is working for you:
$user = $this->sentry->register(array(
'username' => e($data['username']),
'email' => e($data['email']),
'password' => e($data['password'])
));
So you already have it instantiated and you can surely do:
$adminGroup = $this->sentry->findGroupById(5);

Session, PHP Incomplete Class

I am using cakePHP 2.x . Currently doing about the twitter OAuth, http://code.42dh.com/oauth/.
function twitter_authentication()
{
//assume above coding is all correct.
$this->Session->write('twitter_request_token', ($requestToken));
$this->redirect('http://api.twitter.com/oauth/authenticate?force_login=true&oauth_token='.$requestToken->key); //I able to get $requestToken.
}
function twitter_login()
{
$requestToken = $this->Session->read('twitter_request_token');
$accessToken = $this->OAuthConsumer->getAccessToken('Twitter','https://api.twitter.com/oauth/access_token', $requestToken);
At function_login(), I failed to read session and ended up with PhP Incomplete Class. If I do $this->Session->write('twitter_request_token', serialize($requestToken)); and $requestToken = $this->Session->read(unserialize('twitter_request_token'); it will work, but I will ended up error at other places which caused by using serialize and unserialize session.
"PHP Incomplete Class" means PHP doesn't have a class definition for the object you're loading.
Option A: figure out what class that object is when you write it into the session and ensure that class's definition is loaded before loading the object.
Option B: convert the object to an stdClass or array before writing it, and convert back after loading. This might be more complex than the first option.
OAuth.php's OauthToken class is quite simple with just two properties: key and secret. When you get the login url, you can store it to the session as an array:
CakeSession::write('Twitter.requestToken', array(
'key' => $requestToken->key,
'secret' => $requestToken->secret
));
Then, instantiate your own OAuthToken when calling OAuthClient->getAccessToken() like so:
$sessionRequestToken = CakeSession::read('Twitter.requestToken');
$accessToken = $twitterClient->getAccessToken('https://api.twitter.com/oauth/access_token',
new OAuthToken($sessionRequestToken['key'], $sessionRequestToken['secret']));
Should be ready to go:
if ($accessToken) {
$twitterClient->post($accessToken->key, $accessToken->secret,
'https://api.twitter.com/1/statuses/update.json', array('status' => 'My balls smells like A-1 sauce. #science'));
}

Categories