Module Based Login And User Level Acces System in yii - authentication

i have implemented module based login from
here
by update 2 method and it works fine for me .
Now there is need to access this module as different role,
i implement user level access to this module as described here
i add LevelLookUp class to EWebUser.
when i try to login now a CException
"CWebUser and its behaviors do not have a method or closure named "isAdmin". "
displayed.
it means module is still using CWebUser, but i create my own EWebUser and place it to module/components/EWebUser.php
and code in module config file init method
$this->setImport(array(
'bgadmin.models.*',
'bgadmin.components.*',
));
$this->setComponents(
array(
'errorHandler' => array(
'errorAction' => 'bgadmin/default/error'),
'user' => array(
//'class' => 'CWebUser',
'class' => 'EWebUser',
'loginUrl' => Yii::app()->createUrl('bgadmin/default/index'),
)
));
Problem-
1.where to place LevelLookUp class
2.why module is still using CWebuser instead of EWebUser ?
RETRY------------
when i change
$this->setComponents(
to
Yii::app()->setComponents(
then it access the EwebUser but not the loginurl as i logged in but now 2 PROBLEMS
1.'expression'=>'Yii::app()->user->isAdmin()' not works but in class EwebUser it return flase.
2.when logout give error "Property "BgadminModule.user" is not defined. "
logout code is
Yii::app()->user->logout(false);
$this->redirect(Yii::app()->getModule('bgadmin')->user->loginUrl);
in previous case logout was working fine.
Please help me .

When you call $this->setComponents() in a module then actually you set a new component for
you module so to access user component for your module you have to write the following
statement
Yii::app()->getModule('bgadmin')->user->isAdmin() or
Yii::app()->getModule('bgadmin')->user->id
for logout in your admin module you have call it in this way
Yii::app()->user->logout(false);
This will work fine if you are using $this->setComponents() method.
If 'class' => 'EWebUser', doesn't call your class then try to change path as
'class' => 'application.modules.bgadmin.components.EWebUser',
This should work for your bgadmin module

Related

Yii The view file does not exist - where do mail layouts go

I am using basic template but have adapted the advanced user password reset functionality, for some reason I can't get it to find the mail layouts.
So in \mail\layouts I have
- passwordResetToken-html.php
- passwordResteToken-text.php
In web.php I have
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => 'app\mail\layouts',
...
The advanced template uses
'viewPath' => '#common/mail',
But as i'm using basic template, its not in the common/mail folder.
In sendMail function in PasswordResetRequestForm.php i have
return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])
...
However getting error
The view file does not exist: app\mail\layouts\passwordResetToken-html.php
I know this is going to be something small but for the life of me i cannot see it
Removed the 'viewPath' of config, as the doc don't use it.
So it works.

Yii Console Application does not auto-import Model as web appliccation

Here is my console.php which is same as main.php
return array(
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'name' => 'My Console Application',
// preloading 'log' component
'preload' => array('log'),
// autoloading model and component classes
'import' => array(
'application.models.*',
'application.components.*',
'application.extensions.*',
),
The web application has no problem access those functions. However, console application yields error
Fatal error: Call to undefined method Myfunction::get_all_recipients()
An discussion of this problem is also found at
https://github.com/yiisoft/yii/issues/2344
Also tried suggestions
Yii Command Line Does Not Autoload Models
but it still does not import model classes.
The second solution should work, but sometimes I use another approach. In the console command itself add the following:
Yii::import('application.models.YourModel');
or
Yii::import('application.models.Subfolder.YourModel');
if it's located in subfolder for example;
This should placed before your code is processed. This autoloads only models or classes that your need for current command.

can't display error page layouts in yii

I am able to render my specified error pages in my modules, but I can't get error layouts outside of my modules.
It's quite strange, in my main config I have:
'errorHandler'=>array(
'errorAction'=> '//site/error',
),
I tried to see if it is pointed to the right file by changing it to //site/contact, and see if it renders the contact page if error. And it does show.
So the path is correct, then how come it's not showing the error page? It shows the default white page with exception error. The exception page is what im expecting, but how it's formatted is not.
public function actionError()
{
$this->layout = '/layouts/main';
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', array('error'=>$error));
}
}
Your user role doesn't have rights to access actionError() in controller.
In your site controller, You can see a function called public function accessRules().
Check error is added for some user role, or it's for admin only.
If error not found, then add it for user roles.
Example:
public function accessRules()
{
return array(
array('allow',
'actions' => array('index', 'view', 'error'),
'users' => array('#')
),
..
..
..
}
You need to create a errorXXX in views/system. The XXX stand for the error code, in this case most likely a 403. For more information see this page on the Yii site.

Using Elfinder extension in Yii

I need to use a file manager in pop up which comes on a button click. I am using Yii extension elfinder. I am finding it hard to understand the way of using it. I downloaded the code from bitbucket, put it inside my application in the folder extension. I try to test it using new controller, named it elfcontroller and put the following code (got from the website)
class ElfinderController extends CController
{
public function actions()
{
return array(
'connector' => array(
'class' => 'ext.elFinder.ElFinderConnectorAction',
'settings' => array(
'root' => Yii::getPathOfAlias('webroot') . '/uploads/',
'URL' => Yii::app()->baseUrl . '/uploads/',
'rootAlias' => 'Home',
'mimeDetect' => 'none'
)
),
);
}
}
and i created one more function for rendering the index page(i want the file manager to be in this page)
in the view i wrote the following code
$model = new xxxmodel();
$this->widget('ext.elFinder.ElFinderWidget', array(
'model' => $model,
'attribute' => 'serverFile',
'connectorRoute' => 'admin/elfinder/connector',
)
);
and i included a div for containing it
But i am getting the following error
Alias "ext.elFinder.ElFinderWidget" is invalid. Make sure it points to an existing PHP file and the file is readable.
i tried to include alias in config/main.php
I know i am messing some where with the folder structure
here is the path i am using the extension
C:\xampp\htdocs\project\protected\extensions\ext.elfinder
I returned empty after google searches, can any one please explain me how to use this extension to place the code exactly where it is needed to be?
In general the extensions folder already has the ext alias, so you don't need to set an alias for it.
Then the extension itself should be placed in the extensions folder, somewhat like : project/extensions/extension-name/ . In your case it should be: project\extensions\elFinder , and keep the rest of your code same, i.e continue referring to extension like:
ext.elFinder.ElFinderWidget

Can a module have its own config file?

I just want to know if we can add a config file that extends the main.conf in the module
It does, already. in the CWebModule through $this->setComponents as follows:
<?php
class AccountModule extends CWebModule
{
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
$this->setComponents(array(
'errorHandler' => array(
'errorAction' => 'module/default/error'),
'defaultController' => 'default',
'user' => array(
'class' => 'ModuleWebUser',
'allowAutoLogin'=>true,
'loginUrl' => Yii::app()->createUrl('module/default/login'),
)
));
// import the module-level models and components or any other components..
$this->setImport(array(
'module.models.*',
'module.components.*',
));
}
} ?>
The way to do it is to make an array item for your module/etc in the params item of the main config array.
Look at this forum post: http://www.yiiframework.com/forum/index.php/topic/24617-custom-configuration/
if you want your configuration to be in a separate file you can merge it with the main config array in the config file!
something like this should work:
include("custom_config.php"); // define $array_from_custom_conf inside this file
return array_merge(
array(/*main_config_array from main.php*/),
$array_from_custom_conf
);
If you put your custom config array as the 2nd argument it will also overwrite attributes from the main config.
I've never did it but:
A current solution is provided in a wiki article.
Regarding this 'feature request', its not a big surprise that this was already requested on Yii's forums. See here and here.