Add role dynamically without db while logging in in Yii2 - yii2

How can I add a role to a user at the time of log in based on his username. I think it can be accomplished using yii\rbac\PhpManager . I tried something like
$r=new PhpManager;
$r->init();
$r->createRole("1","admin");
$r->save();
$r->assign('1','admin');
where 1 is user id and admin is the intended role.
But I am getting an error
Unknown Method – yii\base\UnknownMethodException
Calling unknown method: yii\rbac\PhpManager::save()
What is the error here? or which is the right method?

Do not call ->save, there is no save function for PHP manager, if you remove $r->save(); it should all work ok I believe.
http://www.yiiframework.com/doc-2.0/yii-rbac-phpmanager.html
In the official docs they never call save() on anything
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

Related

Blockstack-Radiks: Missing signing keys

I am adding Radiks to my blockstack project. When saving the first model I receive the following error:
Unhandled promise rejection TypeError: "First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object."
fromObject index.js:311
from index.js:137
from index.js:149
getPublicKeyFromPrivate keys.js:25
signECDSA ec.js:139
sign model.js:249
save model.js:157
It looks like that a userGroupKey is missing. Where do I need to specify it? The configuration is just a fake radiks server url. Does that matter?
You need to have at least a local radiks server running.
The issue for me was that I didn't call User.createWithCurrentUser on each page. I did on the login page when handlingPendingSignIn but I didn't on pages when the user was already signed in (after isUserSignedIn). Adding User.createWithCurrentUser on these pages solved the problem.

Add additional validation for logins using LexikJWTAuthenticationBundle

I want to add additional validation to allow the login, ie, not just check that the username and password match but do other validations on the user before allowing him to login.
I tried extending JWTTokenAuthenticator but it seems none of its methods are called during the login.
I thought of using a custom "AuthenticationSuccessHandler" but I'm not sure if this is the place I should do this and how could I report from there that the "login" is actually invalid.
Where should I put this logic?
I end up replacing the "login_form" authentication shown on the LexikJWTAuthenticationBundle bundle documentation for a custom guard authentication just for the login, i.e. different than the guard authentication used to validate the JWT tokens.
In the past, I had a similar requirement. Within the alternatives I chose to implement a custom user provider. Symfony documentation on custom user provider creation is more than enough to accomplish the task. Moreover, "Configuring the user provider" section of the LexikJwt documentation explains how to configure lexik_jwt accordingly.

PHP: phpinfo function gives "no value" for some fields

I recently went to some hosting provider website, registered and so on.
At one moment, I tried to run some functions, with no result at the end.
Then I decided to see phpinfo() page, and I have noticed, that, in certain fields,
there is declared as:
no value
In this case, I was interesed in value of:
**
disable_functions : no value
**
Does that mean, that there is really not declared what functions are disabled, or that field is not showing any value as it is managed to not show value in server configuration?
In example:
Functions like shell_exec, fopen and so on are disabled, but server is configured that way to not show me that? Or it really does not have any value?
no value means php.ini contains no value for that directive. The user who is running the server or php process are disabled to run those functions (shell_exec, fopen) or system calls that those functions invoke.

Error while creating a custom entity record in mscrm 4.0

I am new to using CRM.I am facing a poblem while creating custome entity record using a console application.
This console application is run with a user having "systemadministrator" privileges.
When I call objService.Creat(entity); method
I am getting the following exception
0x80042f09
SecLib::CheckPrivilege failed. Returned hr = -2147209463, User: 395a4c3e-cf59-de11-8e41-001a646ad2f9
Please let me know what caused this issue.
Thaks in advance.
Its probably because the actual webservice call is not being performed as a system administrator.
I would suggest using a WhoAmI query to check which user is actually being used, then you can correct their permissions.

sfErrorNotifierPlugin: The "default" context does not exist

I have installed the sfErrorNotifierPlugin. When both options reportErrors/reportPHPErrors reportPHPWarnings/reportWarnings are set to false, everything is ok. But I want to catch PHP exceptions and warnings to receive E-mails, but then all my tasks fail, including clear-cache. After few hours of tests I'm 100% sure that the problem is with set_exception_handler/set_error_handler.
There's a similar question:
sfErrorNotifierPlugin on symfony task but the author there is having problems with a custom task. In my case, even built-in tasks fail.
I haven't used sfErrorNotifierPlugin, but I have run into 'The “default” context does not exist.' messages before. It happens when a call is made to sfContext::getInstance() and the context simply doesn't exist. I've had this happen a lot from within custom tasks. One solution is to add sfContext::createInstance() before the call to sfContext::getInstance(). This will ensure that a context exists.
There's an interesting blog post on 'Why sfContext::getInstance() is bad' that goes into more detail - http://webmozarts.com/2009/07/01/why-sfcontextgetinstance-is-bad/
Well, the problem could not be solved this way, unfortunately. Using sfErrorNotifierPlugin, I have enabled reporting PHP warning/errors (apart from symfony exceptions) and this resulted in huge problems, e.g. built-in tasks such as clear-cache failed.
The solution I chose was to load the plugin only in non-task mode (project configuration class):
public function setup()
{
$this->enableAllPluginsExcept('sfPropelPlugin');
if ('cli' == php_sapi_name()) $this->disablePlugins('sfErrorNotifierPlugin');
}
WHen a task is executed, everything works normally. When an app is fired from the browser, emails are sent when exception/warning occurs (maybe someone will find it useful).
Arms has explained the problem correctly. But usually context does not exist when executing backend/maintenance tasks on the console. And it is easier if you handle the condition yourself.
Check, if you really need the context?
If you do, what exactly do you need it for?
Sometimes you only want a user to populate a created_by field. You can work around by hard-coding a user ID.
If you want to do something more integrated, create a page (which will have a context) and trigger the task from there.
you can test the existance of the instance before doing something inside a class. Like:
if(sfContext::hasInstance())
$this->microsite_id = sfContext::getInstance()->getUser()->getAttribute('active_microsite');
I've been experiencing the same problem using the plugin sfErrorNotifier.
In my specific case, I noticed a warning was raised:
Warning: ob_start(): function '' not found or invalid function name in /var/www/ncsoft_qa/lib/vendor/symfony/lib/config/sfApplicationConfiguration.class.php on line 155
Notice: ob_start(): failed to create buffer in /var/www/ncsoft_qa/lib/vendor/symfony/lib/config/sfApplicationConfiguration.class.php on line 155
So, checking the file: sfApplicationConfiguration.class.php class, line 155,
I've replaced the ' ' for a null, then the warnings disappears, and also the error!
ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : ''); bad
ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : null); good