In Yii 1, I had this code in an API controller to set the active user:
Yii::$app->user->id = $my_userid;
But in Yii2, this is not allow, as Yii::$app->user->id is now read-only.
What would be the equivalent command to set active user id?
You need to use setIdentity() method for API:
use Yii;
...
Yii::$app->user->setIdentity($user)
setIdentity(): changes the user identity without touching session or
cookie. This is best used in stateless RESTful API implementation.
where $user should be valid instance of your User model.
If you need to change existing user, use switchIdentity() method instead:
use Yii;
...
Yii::$app->user->switchIdentity($user)
Related
Can anyone provide basic examples of how to customize a feathers service to perform mongodb aggregation pipeline? I have tried it with hooks but I lost in the midway.
You can create Custom Service in FeathersJs where in the class you can use aggregation or also in hooks you can use it.
EX :
I have service users I want to get data through aggregation.
const userData = context.app.service('users').Model.aggregate([
// Use Your pipelines here
]);
I am writing an app that runs from the browser. However, some model functions are also called from the Yii2 console. Therefore, I am getting errors when trying to access variables that are set in the GUI.
Is it possible to tell which mode I am in? Is there some environment variable automatically set, or should I just set some session variable in the console app to indicate the state?
You can use
if (Yii::$app instanceof \yii\console\Application)
for console, and
if (Yii::$app instanceof \yii\web\Application)
for web.
Correct variant
Yii::$app->request->isConsoleRequest
There is a simpler way to figure this out without going through the Yii objects
if (php_sapi_name() == "cli") {
return;
}
...and it works for all PHP scripts
...and it is lighter
By default for console:
Yii::$app->id == 'basic-console'
And for web application:
Yii::$app->id == 'basic'
Yii::$app->id stores the id of the loaded configuration params. By default for console application it is 'basic-console' and for web application it is 'basic' (defined in configuration file)
Yii2 provides a number of different classes for application's console and for those of type web. In addition to this division of the mode of operation of the classes, there are also a set of rules governing the organization of the code of the application. The first, fundamental, it is the respect of giving the MVC Model object information, to view the management interface with the user and, finally, to the controller the role of coordination among them. In your case it seems to sense that a piece of code runs in console but referring to classes that provide a Web interface. Probably because in some Model classes were introduced with functions with HTML or other code that should not be there. If you need two separate applications should precisely separate applications that use a type controls
yii\console\Controller
and another that uses controller type web
yii\web\Controller.
Obviously Model classes will be common and, thanks to separate controller, be sure to invoke View appropriate to the type of user interface in use. I Hope this could be useful.
Works for nginx and apache:
function isConsole()
{
return 'cli' == php_sapi_name() || !array_key_exists('REQUEST_URI', $_SERVER);
}
Pure PHP:
global $argv;
if (empty($argv)) {
// Browser mode
}
else {
// Cli mode
}
I have a question what would be the correct way to define methods within controllers.
I see many tutorials on using (index, show, create, store, edit, update, destroy) and in another use the following (getIndex, getShow, getCreate, postCreate, getEdit, postEdit).
the wake of this would like to know from the experts, which model to follow ?.
This is not really a Laravel problem and there is no "correct" way for a Laravel application, it's up to you, but there are some rules to follow in some cases.
Resourceful Controller Methods
The methods you listed are basically the Rails restfull ones:
index, show, create, store, edit, update, destroy
And they are automatically created by Artisan when you do:
php artisan controller:make ControllerName
Also a Laravel Restful route will expect them:
Route::resource('post', 'PostController');
RESTful Controllers
Here Laravel will try to automatically guess the kind of HTTP method your controller accepts and build all routes for you. So, if you have a route:
Route::controller('PostController');
And your PostController class having a method
public function getIndex()
{
...
}
You are telling Laravel to create a route GET to that method.
It's up to you
If you create all your routes manually, you could do:
Route::get('users', 'UsersController#usersIndex');
Pointing to
public function usersIndex()
{
...
}
And Laravel will not force you do any of those others ways.
Docs
Take a look at the docs: http://laravel.com/docs/controllers#restful-controllers
Here's some documentation about this in Rails: http://guides.rubyonrails.org/routing.html
In CocosBuilder, there is a Code Connections section. At second line, it has three options: Don't Assign, Doc root var and Owner var.
Sometimes, I got a error when I was selecting Owner var, but it works fine after I changed it to Doc root var.
I google a lot, but can't find a satisfied answer.
Does anyone can explain the difference clear?
Don't assign simply means that you are not using the Code Connections.
Doc root var means that you are connecting a custom cocos2d class. This will glue/connect the object in your document (CCB stage/file) to your code. This option is convenient but you must make sure that root node's controller object is provided.
Sometimes you need to be able to access member variables from and get
callbacks to another object than the root node of a ccb-file. To do
this you will need to pass an owner to the CCBReader.
as explained in Connecting with cocos2d-x.
Owner var provides you with more flexibility by allowing you to connect to a variable other than the root node. You can glue it to any variable of your choosing.
The error you are getting is most likely caused by providing a name that is not available (the variable doesn't exist). Note that setting the property to Doc root var or Owner var and leaving the field empty will cause this error.
When linking member variables the Doc root var will add a member in the root node's controller object. You could access it via MainScene.myVar assuming that you JS Controller is MainScene. This is defined by your scene root layer JS Controller property.
Alternatively, you can do the same thing with a custom object that is not directly tied to the scene via the JS Controller connection. To achieve this, you would use the Owner var attribute.
Don't Assign is the default and doesn't do anything.
In essence, these features allows you to easily reference those CCB objects from your code.
Experimenting with the CocosBuilder JS Example Games may be of help. The documentation on how to connect with cocos2d-x might also be useful to you if you didn't read it yet.
I have a controller that has many actions and it specifies a default class-level #PreAuthorize annotation, but one of the actions I want to let anyone in (the "show" action).
#RequestMapping("/show/{pressReleaseId}")
#PreAuthorize("permitAll")
public ModelAndView show(#PathVariable long pressReleaseId) {
ModelAndView modelAndView = new ModelAndView(view("show"));
modelAndView.addObject("pressRelease",
sysAdminService.findPressRelease(pressReleaseId));
return modelAndView;
}
Unfortunately, Spring Security throws this exception:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:321)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:195)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
How can I get Spring Security to NOT throw this exception? I just want to let anyone in - non-authenticated users and authenticated users - Everyone.
My only solution is to put this method into another controller altogether with no #PreAuthorize at all... which will work, but that's stupid. I want to keep all my press release actions in the same controller.
Thanks for the help!
I guess you don't have anonymouse authentication filter enabled.
If you use namespace configuration and auto-config = "true", it should be enabled by default. If you don't use auto-config, you can enable anonymous filter as <security:anonymous />.
The normal way to allow anyone to access a controller method is to not annotate it with any Spring Security annotations; ie no #PreAuthorize no #Secured etc.
You should be able to simply remove #PreAuthorize from the show() method, any leave the annotation on the other methods in that controller. The other methods will remain secured regardless of what you do with the show() method.
The default changed with a newer version (version 2) of the spring security plugin. That means all controllers are secured by default (you need to login to see them). Removing #Secured will not work at all, it will still stay secured. You can change this default behaviour, but to me the new default behaviour makes sense because it is much more secure. And security is very important.
I only use
#Secured(['permitAll'])
for my controller, but it should also work for a method.