Laravel Entrust, how to attach existing permission to role - php

Here is my code I am using, it runs fine but when I'm inside the database checking if everything went okay I get this result
Why am I getting 0 for the permission ID and Role ID?
$developer = new Role();
$developer->name = 'Test';
$view_customers = new Permission();
$view_customers->name = 'view_customers';
$view_customers->display_name = 'View Customers';
$developer->attachPermission($view_customers->id);

try this
$developer = Role::where('name','=','Test')->get->first();
//if you want to attach new permission
$view_customers = new Permission();
$view_customers->name = 'view_customers';
$view_customers->display_name = 'View Customers';
//if you want to attach existing permission ex. you already have 'view_customers' in permission table
$view_customers = Permission::where('name','=','view_customers')->get->first();
$developer->attachPermission($view_customers);

attachPermission() works by model or array, if you want atach or detach by id use this
$developer->perms()->attach($view_customers->id);

Related

Laravel Crud Editing makes a new record

I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
I want a check before it makes a new ExtraRegistration. that it only makes a new registration if the registration_id with that extra_id doesn't already exists. (Not 100% sure though).
Thanks in advance!
you make a new object of ExtraRegistration so its always make a new entry for update first get object of those id after that update
check the below link
https://laravel.com/docs/5.8/eloquent#updates
This happens because you're creating a new ExtraRegistration record:
$registrationextra = new ExtraRegistration();
If you want to update it, you need to find the related $registrationextra for your $options, and then update them (assuming you have relations set up):
$registrationextra = ExtraRegistration::where('options_id', $option->id);
$registrationextra->update([
'your_fields' => value
// etc...
]);
If you want to check if ExtraRegistration exists, and depending on that, create or update it, you can do something like this:
$registrationextra = App\ExtraRegistration::updateOrCreate(
['your_fields' => 'value'],
);
You can read more on official documentation.

How to get user id from Zend_Auth

I have big problem with getting user_id with zend..
My auth login code:
if($form->isValid($this->getRequest()->getPost())){
$adapter = new Zend_Auth_Adapter_DbTable(
null,
'sprzedawca',
'email',
'haslo',
'MD5(CONCAT(?, salt))'
);
$adapter->setIdentity($form->getValue('email'));
$adapter->setCredential($form->getValue('haslo'));
$auth = Zend_Auth::getInstance();
$wynik = $auth->authenticate($adapter);
if($wynik->isValid()){
//return $this->_helper->FlashMessenger('Pomyślnie zalogowano');
return $this->_helper->redirector(
'index',
'sprzedawca',
'default'
);
Now when i try write user id using:
Zend_Auth::getInstance()->getIdentity()
I got only 'username' but i want to get user_id
Zend_Auth::getInstance()->getIdentity()->user_id
doesn't work too..
okay, i didn't store item while loggin
Should be:
if($wynik->isValid()){
$storage = $auth->getStorage();
$storage->write($adapter->getResultRowObject('sprzedawca_id'));
and now works perfectly
I can't see your database Scheme now, but i can tell you that this has worked for me.
<?php
$auth = Zend_Auth::getInstance();
$userId = $auth->getIdentity()->id;//user_id OR id depending your primary key of the user table.
echo $userId;
?>
Attention:- $userId = $auth->getIdentity()->id; //id must written as it is your user table, other it wise won't work
I managed to get this through a Google search, try it.
Zend_Auth::getInstance()->getIdentity()->User_ID

Magento - Programatically retrieve and set admin users status

For a custom extension I'm building I need to be able to set and retrieve the status of an administrator user (not a customer) in Magento. I imagine you could achieve this like so;
$id; // ID of user stored here
$user = $mage->getadminuser($id); // store the user as an object or array in a variable with ID
$user->getStatus(); // return either true or false?
$user->setStatus(active or not active); // activate or deactivate the user
If anyone could provide me with the code to do this or documentation where I can find this easily?
Thanks!
$id = 5;
$admin = Mage::getModel('admin/user')->load($id);
if ($admin->getId()){
$admin->setIsActive(1);//or 0
$admin->save();
}

Custom user-roles and -permissions in installation script

Is it possible to write some code in your .install-file of your D7 website which allows you to generate user roles and permissions automatically? I always though so, but right now, I can't think of a way to do it.
Any advice?
Absolutely:
function mymodule_install() {
// Make the new role
$role = new stdClass;
$role->name = 'new role name';
$role->weight = 3;
user_role_save($role);
// Permissions to assign to the role.
// Note these are defined in hook_permission()
$perms = array(
'access administration pages',
'view content',
'any other permission you want'
);
// Grant the permissions. This function takes care of all necessary cache resets
user_role_grant_permissions($role->rid, $perms);
}

Where is the action.class in sfDoctrineGuardPlugin for create new user?

I installed sfDoctrineGuardUser for Symfony 1.4.11, but I can't find the action.class, where register user. I find only class sfGuardCreateUserTask :
protected function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager($this->configuration);
$user = new sfGuardUser();
$user->setEmailAddress($arguments['email_address']);
$user->setUsername($arguments['username']);
$user->setPassword($arguments['password']);
$user->setFirstName($arguments['first_name']);
$user->setLastName($arguments['last_name']);
$user->setIsActive(true);
$user->setIsSuperAdmin($options['is-super-admin']);
$user->save();
$this->logSection('guard', sprintf('Create user "%s"', $arguments['username']));
}
but this isn't this...
I can't find anywhere for example $user->setFirstName($arguments['first_name']); and modified to:
$user->setFirstName($arguments['first_name'] . '#');
Where is the action.class in sfDoctrineGuardPlugin for create new user?
Basically, you don't need that action (or to customize it) in order to create new users. Just create a custom action that generates your registration form and handles the post:
$user = new sfGuardUser();
$user-> // manipulate however you like
$user->save();
try
cache\frontend\prod\modules\autoSfGuardUser\actions
or something similar
if this is what you're looking for, don't rewrite it in there!!
Instead, copy the action to your normal sfGuard module and edit it there
Everything you need is already published by the symfony team :
http://symfony.com/blog/call-the-expert-simple-registration-with-sfdoctrineguardplugin
for further information : http://symfony.com/blog/call-the-expert-customizing-sfdoctrineguardplugin

Categories