I have just discovered and started using Role Based Access control.
Since I am using an advanced template for yii2, I am wondering if roles and permissions are shared between backend and frontend tiers or if they are separated.
For example
<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;
// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);
}
}
would author and createpost be available for both backend and frontend?
thank you!
The RBAC componet are base on common part .. typically if they are base on DB you use common models and shared the related db table ..
You can declare this element in component section of main.php in cofig area and if you do this in common dir this component si correctly shared between both the enviroment (frontend , backend) and eventually between all apps you distribute you projectc..
eg : common/config/main.php
'components' => [
.....
'authManager' => [
'class' => 'yii\rbac\DbManager',
'cache' => 'cache',
....
],
this mean they could be naturally shared between frontend and backend ..
Related
I have a module which uses a secondary database. In it, I am trying to log in to the user table from that secondary database. The problem is that the \Yii::$app->user->identity->id is using the first database. How should I override the class to do it like this? What I got in my LoginForm.php in the module is :
public function login()
{
if ($this->validate() && $this->user) {
$isLogged = Yii::$app->getUser()->login($this->user, $this->rememberMe ? $this->module->rememberFor : 0);
//var_dump($this->user);exit;
if ($isLogged) {
$user = \frontend\modules\store\models\User::findOne(Yii::$app->user->identity->id);
$user->last_login_at = time();
$user->update();
// $this->user->updateAttributes(['last_login_at' => time()]);
}
return $isLogged;
}
return false;
}
As you can see the user class here is overridden and it is using the secondary database. But how should I override the Yii::$app->user->identity->id to use this database also? Thank you in advance!
As you are using Yii2 advanced template, you should consider adding a new sub application. Yii2 advanced template allows you to have different sessions for frontend and backend sub applications. Advanced Template on Same Domain and different sessions
Similarly, you can add a new app, in your case it may be called store. If you do it as a separate app, you can simply override identity class and even have different model for user. Help about adding new app is here.
You can override user identity in config
'user' => [
'identityClass' => 'app\models\User', // User must implement the IdentityInterface
'enableAutoLogin' => true,
// 'loginUrl' => ['user/login'],
// ...
]
more info here
I'm kinda a newbie in Cakephp (3.5) and I'm currently trying to make my first plugin (called Example) which contains several sub-directories. One of them is the UserManager directory which contains a Users MVC standard suit with authentication.
Since I want to add social logins and other stuffs, I created my own auth component as explained in the docs :
plugins/Example/UserManager/src/Controller/AppController.php
<?php
namespace Example\UserManager\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Example/UserManager.Example' => [
'fields' => ['username' => 'email', 'password' => 'pass'],
'userModel' => 'Users',
],
],
]);
}
}
plugins/Example/UserManager/src/Auth/ExampleAuthenticate.php
<?php
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
class ExampleAuthenticate extends BaseAuthenticate
{
// The same as Form authentication, since I'm testing
}
The problem is that I can't make the authentication component find the ExampleAuthenticate class. I already tried by setting the authenticate config param like
Example
ExampleAuthenticate
UserManager.Example
Example/UserManager.Example
Example\UserManager.Example
Example/UserManager.ExampleAuthenticate
Example\UserManager.ExampleAuthenticate
but I always get the error Authentication adapter "..." was not found. when visiting http://localhost/Project/example/user-manager/users :(
Does anyone have any clue of what I might be missing?
The problem was that the php function class_exists(...) didn't recognise the custom authentication class, so after digging a bit more I realised that the namespace shown in the docs only works for a custom authentication file defined in the App environment, but not in the Plugin one (silly me).
So I changed namespace App\Auth; to namespace Example\UserManager\Auth; inside ExampleAuthenticate.php and it worked like a charm! now the function class_exists('Example\\UserManager\\Auth\\ExampleAuthenticate') returns true and everything works perfect by defining Example/UserManager.Example in the authenticate config params.
I am trying to make CRUD of all database tables in APP controller instead of all CRUD related functions in inherited controllers.
For example:
www.example.com/settings/edit/1
www.example.com/users/edit/1
www.example.com/news/edit/1
I want to shift this Edit functionality to App controller.
In that case, because of all controllers (settings, users, news) are inherited from APP so they can access AppController's method EDIT.
I want to access this without creating a file of inherited controller.
I am trying with this routes.
$routes->connect('/:table/edit/:recordId', ['controller' => 'app', 'action' => 'edit'],['pass' => ['table', 'id'], 'id' => '\d+']);
But its showing error
Missing argument 2 for App\Controller\AppController::edit()
while in controller I wrote :
public function edit($table, $recordId){
}
Any help regarding this will be really appreciable.
Thanks
I can't put the library under vendor/ because that directory is ignored in .gitignore. I put it under bing-ads/ and I have
<?php
namespace app\models;
include 'bing-ads\v10\bingads\ClientProxy.php';
use \Yii;
use BingAds\Proxy\ClientProxy;
to access it. It works for console commands/actions, but I have a feeling it will not work during a web call because the root directory will be web/. Where should I put this library and how can I access it via both console actions and web actions?
The Microsoft PHP library is located here.
I found a way by adding the file paths to the autoload section of composer.json. I remembered that I had to do that for some of the other libraries as well, even the ones available via Composer.
"autoload": {
"classmap": [
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util",
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/Util/v201605",
"bing-ads/v9/bingads/CustomerManagementClasses.php",
"bing-ads/v10/bingads/v10/CampaignManagementClasses.php",
"bing-ads/v10/bingads/v10/BulkClasses.php",
"bing-ads/v10/bingads/ClientProxy.php"
]
}
Then I ran
$ composer install
...
Generating autoload files
I'm not sure this is the best way though.
You could store them wherever you want e.g. in a folder named "BingAds".
Just add the alias as example in a common base config file /common/config/base.php which is included in your console as well as in your web application e.g. for your /web/index.php
$config = \yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/base.php'),
require(__DIR__ . '/../common/config/web.php'),
require(__DIR__ . '/../config/base.php'),
require(__DIR__ . '/../config/web.php')
);
Inside this common/config/base.php you could add your settings, extensions etc. which are valid for both the console and your web application
<?php
$config = [
...
'aliases' => [
'#BingAds' => '#app/BingAds/v10',
'#BingAds/Proxy' => '#app/BingAds/v10/bingads',
],
];
Your Clientproxy.php is stored as example in the directory /BingAds/Proxy/.
Then you don't have to include your files every time you want to use them and just write.
use BingAds\Proxy\ClientProxy;
I have unpacked your linked zip file and stored the files of the directory Bing Ads API in PHP\PHP\Bing Ads API in PHP to my application root directory BingAds whith the aliases mentioned above.
I have tested it by creating a clientProxy object in both a console and web application.
$test = new ClientProxy('test');
var_dump($test);
Both Printed out
object(BingAds\Proxy\ClientProxy)[140]
private 'authenticationToken' => null
private 'username' => null
private 'password' => null
private 'developerToken' => null
private 'wsdlUrl' => string 'test' (length=4)
private 'accountId' => null
private 'customerId' => null
private 'service' => null
private 'namespace' => null
I haven`t tested out other classes but I guess you get how it works.
Add this to your composer.json:
{
"repositories": [
{
"type": "package",
"package": {
"name": "microsoft/bing-ads",
"version": "9.0.0",
"dist": {
"url": "https://code.msdn.microsoft.com/Bing-Ads-API-Version-9-in-fb27761f/file/159208/2/Bing%20Ads%20API%20in%20PHP.zip",
"type": "zip"
},
"autoload": {
"classmap": [
"PHP/Bing Ads API in PHP/v10/bingads/"
]
}
}
}
],
"require": {
"microsoft/bing-ads": "9.0.0"
}
}
Then add composer generated autload.php file if you haven't already.
Then you can call the BingAds\Proxy\ClientProxy() without includes.
There's no better way:)
It's best to place external library to extensions directory.
And in config do
return [
'import' => [
'application.extensions.bing-ads.v10.bingads.ClientProxy',
],
...
];
in main and in console configs.
I usually just put the code in a components folder. If you're using the basic template, this folder can be under your application root; if you're using the advanced template, this folder can be under the necessary app: frontend, backend, console, or common. I recommend putting it under common for reasons I shall explain later
Under every app config, Yii 2 uses the ::setAlias method to assign different aliases to the key folders. In the basic app template, #app refers to the application root. While in the advanced template #app may refer to any of backend, frontend, or console.
If your library code is under backend, you can access it like so
namespace backend\controllers;
use yii\web\Controller;
use backend\components\MyLibCode;
class SiteController extends Controller
{
public function actionIndex()
{
$mlb = new MyLibCode();
// ...
}
}
However, it is good Yii 2 practice to have common library code in the common folder. So if this library code is to be used across apps, I suggest you put it into the common\components folder and replace backend with common in the use statement in the code above.
p.s: Justinas method also works; it's borrowed from Yii 1. But this may become cumbersome because this asks Yii to load the class when the application starts. If there is some heavy-lifting in that file, it may be detrimental to the app's performance.
I've been having a hard time trying to figure out how to load jQuery or other CORE scripts in Yii 2.
In Yii 1 it seemed this was the way:
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
In Yii 2, $app is a property of Yii, not a method, so the above naturally doesn't work, but changing it to:
<?php Yii::$app->clientScript->registerCoreScript("jquery"); ?>
produces this error:
Getting unknown property: yii\web\Application::clientScript
I couldn't find any documentation for Yii 2 about loading core scripts, so I tried the below:
<?php $this->registerJsFile(Yii::$app->request->baseUrl . '/js/jquery.min.js', array('position' => $this::POS_HEAD), 'jquery'); ?>
Whilst this loads jQuery in the head, a second version of jQuery is also loaded by Yii when needed and hence causes conflict errors.
Additionally, I don't want to use Yii's implementation of jQuery, I would prefer to maintain my own and hence that is why I am doing this.
How can I load jQuery and other core files without Yii loading duplicate copies of them when it needs them?
In order to disable Yii2's default assets you can refer to this question:
Yii2 disable Bootstrap Js, JQuery and CSS
Anyway, Yii2's asset management way is different from Yii 1.x.x. First you need to create an AssetBundle. As official guide example, create an asset bundle like below in ``:
namespace app\assets\YourAssetBundleName;
use yii\web\AssetBundle;
class YourAssetBundleName extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'path/file.css',//or files
];
public $js=[
'path/file.js' //or files
];
//if this asset depends on other assets you may populate below array
public $depends = [
];
}
Then, to publish them on your views:
use app\assets\YourAssetBundleName;
YourAssetBundleName::register($this);
Which $this refers to current view object.
On the other hand, if you need to only register JS files into a view, you may use:
$this->registerJsFile('path/to/file.js');
yii\web\View::registerJsFile()
And if you need to only register CSS files into a view, you may use:
$this->registerCssFile('path/to/file.css');
yii\web\View::registerCssFile()
You can remove the core jQuery from loading like so:
config/web.php
'assetManager' => [
'bundles' => [
// you can override AssetBundle configs here
'yii\web\JqueryAsset' => [
'sourcePath' => null,
'js' => []
],
],
],