0

In magento 1 I did the below code, which worked fine.

Step 1: Fetch all the active api user list try{ require_once '../app/Mage.php'; Mage::app();

        $usermodel = Mage::getModel('api/user')->getCollection()->addFieldToFilter('is_active', 1);
        $id = $usermodel->getColumnValues('user_id');$name = $usermodel->getColumnValues('username');
        $api_key = $usermodel->getColumnValues('api_key');
        echo '<pre>';print_r($api_key);exit;

        $ulist = array_combine($id,$name);
    }catch(Exception $e){
        echo $e->getMessage();
    }


$rolemodel = Mage::getModel('api/role')->getCollection();
$role_id = $rolemodel->getColumnValues('role_id');
$role_name = $rolemodel->getColumnValues('role_name');
$list = array_combine($role_id,$role_name);

Step 2: $_GET['id'] = $apiUserId; Fetch role that belong to a given api user id.

if(isset($_GET['id'])){
    $usermodel = Mage::getModel('api/user')->getCollection();
    $id = $usermodel->getColumnValues('user_id');$name = $usermodel->getColumnValues('username');
    $ulist = array_combine($id,$name);


    $role_id_under_selected_user = Mage::getModel('api/user')->loadByUsername($ulist[$_GET['id']])->getRoles();
    $role_id_under_selected_user = $role_id_under_selected_user[0];
    $rlist = array();
    if($role_id_under_selected_user)
    $rlist[$role_id_under_selected_user] = $list[$role_id_under_selected_user];     
}

I need to do the same in magento 2. Can anyone help?

  • first.Mage:: deprecated at magent 2.magento 1 code is not working on magento 2 – Amit Bera Aug 8 '16 at 14:58
1

In your magento root create UserRole.php

    use Magento\Framework\App\Bootstrap;
    include('app/bootstrap.php');
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $roleAuthModel = $objectManager->create('\Magento\Authorization\Model\Role');
    $userModel = $objectManager->create('\Magento\User\Model\User');


    //list all apiuser
    $usermodel = $userModel->getCollection()->addFieldToFilter('is_active', 1);
    $id = $usermodel->getColumnValues('user_id');
    $name = $usermodel->getColumnValues('username');
    $userList = array_combine($id,$name);//all api user


    //list all role
    $rolemodel = $roleAuthModel->getCollection();
    $role_id = $rolemodel->getColumnValues('role_id');
    $role_name = $rolemodel->getColumnValues('role_name'); 
    $roleList = array_combine($role_id,$role_name);// all role

    //role_id against user_id

     $user_id = 11;//suppose user_id =11
    $role_id_under_selected_user = $userModel->loadByUsername($userList[$user_id])->getRoles();

You get the role_id against username.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.