Dieses Blog durchsuchen

Dienstag, 10. Mai 2016

Create a magento 1 api user programmaticly

If you want to add a user via method you can use the following code

1) the main function in a class Ibrams/Application/Model/User.php
/**
* @param array $userData
*/
public function addApiUser(array $userData)
{
$api_role = Mage::getModel('api/roles')
->setName($userData['roleName'])
->setPid(false)
->setRoleType('G')
->save();
Mage::getModel('api/rules')
->setRoleId($api_role->getId())
->setResources(array('all'))
->saveRel();
$api_user = Mage::getModel('api/user');
$api_user->loadByUsername($userData['userName']);
$data = array(
'username' => $userData['userName'],
'firstname' => $userData['firstName'],
'lastname' => $userData['lastName'],
'email' => $userData['email'],
'api_key' => $userData['apiKey'],
'api_key_confirmation' => $userData['apiKey'],
'is_active' => 1,
'user_roles' => '',
'assigned_user_role' => '',
'role_name' => '',
'roles' => array($api_role->getId())
);
if($api_user->getUserId())
{
$data['user_id'] = $api_user->getId();
}
$api_user->setData($data);
$api_user->save();
$api_user->load($api_user->getId());
$api_user->setRoleIds(array($api_role->getId()))
->setRoleUserId($api_user->getUserId())
->saveRelations();
}
view raw gistfile1.txt hosted with ❤ by GitHub

2) the call
$defaultApiUser = array();
$defaultApiUser['userName'] = 'apiadmin';
$defaultApiUser['email'] = 'admin@test.com';
$defaultApiUser['apiKey'] = 'asecret';
$defaultApiUser['firstName'] = 'apiadmin';
$defaultApiUser['lastName'] = 'apiadmin';
$defaultApiUser['roleName'] = 'apiadmin';
/** @var Ibrams_Application_Model_User */
$userModel = Mage::getModel("application/user");
$api_user = Mage::getModel('api/user');
$userModel->addApiUser($defaultApiUser);
$coreConfig = new Mage_Core_Model_Config();
view raw gistfile1.txt hosted with ❤ by GitHub

Keine Kommentare:

Kommentar veröffentlichen