Domanda

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

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top