Jump to content

Accessing module config fields values


rusjoan
 Share

Recommended Posts

Before all I just want to say that I'm really, really angry.
I've spent ton of hours by solving a lot of stupid issues instead of doing main task of my project...
 
I love PW and exited of his flexibility and usability. But some sides of developing aren't showed so fine.
 
My project works around one main algorithm what I planned to make today but it's impossible because of really stupid behavior of PW and poor docs...
 
Please, help me. I hope in u to make me work faster with your advices.
 
 
So, first my issue... I built module with such structure:
<?

class Oauth extends WireData implements Module, ConfigurableModule {
	public static function getModuleInfo() {
		return array(
			'title' => 'oAuth 2.0 module',
			'summary' => 'First create "token" field in system user template',
			'version' => 100,
			'singular' => true,
			'autoload' => false
		);
	}

	public static function getModuleConfigInputfields(array $data) {
		$inputfields = new InputfieldWrapper(); 

		$field = wire('modules')->get('InputfieldText');
		$field->name = 'getTokenUrl';
		$field->label = "Requesting address to get access token";
		if(isset($data['getTokenUrl'])) $field->value = $data['getTokenUrl'];
		$inputfields->add($field); 

		$field = wire('modules')->get('InputfieldText'); 
		$field->name = 'appID';
		$field->label = 'oAuth application ID';
		if(isset($data['appID'])) $field->value = $data['appID'];
		$inputfields->add($field);

		$field = wire('modules')->get('InputfieldText'); 
		$field->name = 'appSecret';
		$field->label = 'Application secret';
		if(isset($data['appSecret'])) $field->value = $data['appSecret'];
		$inputfields->add($field);

		return $inputfields; 
	}

	public function process($redirectUrl)
	{
		if ( ! isset($_GET['code']) || isset($_GET['error']) ) throw new Exception("Missing get parameter", 1);
$hui = $this->appID;
var_dump($hui);
		$authCode = $_GET['code'];
		$requestUrl = 	$this->getTokenUrl .
						'?client_id=' . $this->appID .
						'&client_secret=' . $this->appSecret .
						'&code=' . $authCode .
						'&redirect_uri=' . $redirectUrl;

		$requestResult = @file_get_contents($requestUrl);
		//$requestResult = json_decode($requestResult, true);

		if ( ! isset($requestResult['access_token']) ) throw new Exception("Missing access token from server: " . $requestUrl .":". $requestResult, 2);

		$tempUser = $pages->get("template=user,name=" . $requestResult['user_id']) or new User();

		$tempUser->name = $requestResult['user_id'];
		$tempUser->token = $requestResult['access_token'];
		$tempUser->pass = md5($tempUser->token);
		$tempUser->addRole('oauth');

		$userapi = new userapi($requestResult['access_token']);

		$requestResult = $userapi->api(
			"users.get",
			['uids' => $tempUser->name, 'fields' => 'first_name,last_name,photo']
			);
		$requestResult = $requestResult['response'][0];

		$tempUser->title = $requestResult['first_name'] . ' ' . $requestResult['last_name'];
		$tempUser->photo = $requestResult['photo'];
		$tempUser->lastvisit = date('H:i d.m.Y', time());

		$tempUser->save();

		$session->login($tempUser->name, md5($tempUser->token));

		return true;
	}

	public function init() {

		$this->setFuel("oAuth", $this);
		require_once($config->paths->templates . "/vkapi.class.php");

		if ( ! $user->isLoggedin() || $user->isSuperuser() ) return;
		if ( empty($user->token) ) $session->logout();

		$userapi = new userapi($user->token);
		$requestResult = $userapi->api(
			"users.get",
			['uids' => $user->name, 'fields' => 'first_name,last_name,photo']
			);

		$requestResult = $requestResult['response'][0] or $session->logout();

		if ( ! $user->isLoggedin() ) return;

		$user->title = $requestResult['first_name'] . ' ' . $requestResult['last_name'];
		$user->photo = $requestResult['photo'];
		$user->lastvisit = date('H:i d.m.Y', time());

	}
}

This module is configurable with 3 fields.

But I can't use this values! I dont know why, because in other mine module all works fine.

This module should check user auth status on every page load (so it's only idea to place these instructions to init() but when I write die() instruction at the beginning of init() — nothing happens, site works as usual... But my suggestion is in that every module initializes after API but before page will load. So docs haven't helped me (forum too).

Next, I call process() method from module in /login/ page, here's full code:

<?

if ( $input->urlSegment1 == "logout" )
{
	$session->logout();
	header("location: /");
	exit;
}

try
{
	$redirectUrl = "http://" . $config->httpHost . "/login/";
	
	$oAuth = $modules->get("Oauth");
	if ( $oAuth->process($redirectUrl) ) echo "Success";
}
catch (Exception $e)
{
	echo "<p>" . $e->getCode() . "</p>" . "<p>" . $e->getMessage() . "</p>";
}

Also I tried to do that by

$modules->get("Oauth")->process($redirectUrl);

But it doesn't matter how I can see.

And now — magic!

1. If I set module autoload = false /login page gives that error:

Error: Call to a member function process() on a non-object

2. When autoload isn't set (not false or true, it isn't defined in config array)

Then process() method doesn't work correctly because I can't take value from config fields.

Issuing $this->appID gives NULL

We can try to define process() as static method but it's obviously not a good idea.

3. autoload = true

Well here quite simple.. Init() method isn't able to require class-file because of wrong path. And path is wrong because $config->paths array is empty and I can't take right path to templates folder.

And, to one heap, I had to use $_GET['code'] because $input->get->code always empty...

Link to comment
Share on other sites

Hi rusjoan,

Sorry you're having a bad experience at the moment, but I don't think it helps to get angry at PW.

Just having a quick look, I see a couple of issues with your code.

  1. $config->paths will be empty because when you're inside a function you need to use wire('config') or $this->config depending on how you are using it in the module. This is because of PHP variable scope - there are lots of posts about this.
  2. Same goes for your use of $user and $input->get

Try fixing those and see where you are at. I think there are probably more things amiss, but let us know what happens with those changes first.

If you are calling the module on a use specific basis in your templates, which I think is what you are doing, then autoload false makes sense.

  • Like 3
Link to comment
Share on other sites

Can't believe it!

Using wire('..') helped me so fine! I knew about it before but forgot to use here. I set module again to autoload=true and all works fine.

OK, now I'm goin' to make init() function do what it should do. I moved all instructions except first two to other function.

So current task is to select proper hook...

Which one is more correct in my situation?

Link to comment
Share on other sites

Let's skip this question, I set hook before of Page::render.

But now funny situation again.

This is the section of module where regular init method is placed:

	public function userProcess()
	{
		if ( ! wire('user')->isLoggedin() || wire('user')->isSuperuser() ) return;
		if ( empty(wire('user')->token) ) wire('session')->logout();

		$userapi = new userapi(wire('user')->token);
		$requestResult = $userapi->api(
			"users.get",
			['uids' => wire('user')->name, 'fields' => 'first_name,last_name,photo']
			);

		if ( ! isset($requestResult['response'][0]) )
		{
			wire('session')->logout();
			return;
		}

		$requestResult = $requestResult['response'][0];

		wire('user')->of(false);
		wire('user')->title = $requestResult['first_name'] . ' ' . $requestResult['last_name'];
		wire('user')->photo = $requestResult['photo'];
		wire('user')->lastvisit = date('H:i d.m.Y', time());
		wire('user')->save();
	}

	public function init() {

		$this->setFuel("oAuth", $this);
		require_once(wire('config')->paths->templates . "/vkapi.class.php");
		$this->addHookBefore('Page::render', $this, 'userProcess');

	}

When I'm adding echo $this->user->token at the beginning of userProcess() for debug purposes, login via oAuth works well (but displays user's token at  the start of page). But when this sentence doesn't exists user authorization doesn't work. My suggestion is in that "echo" issues write cookies that wire('session')->login provides in process() method.

Any suggestions?

Link to comment
Share on other sites

I continue my monolog.

I removed

if ( empty(wire('user')->token) ) wire('session')->logout();

in the start of userProcess( ) because this method is calling before Pare::render by installed hook.

But /login page calls process( ) method while page renders. So $user->token will be empty or wrong at this time. And, as I suggest, session->logout doesn't allow to run session->login on process( ) method.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...