<commit_before><?php
    /**
     * Created by PhpStorm.
     * User: rocks
     * Date: 9/5/14
     * Time: 1:44 PM
     */

    namespace app\rbac;


    use app\models\Distributor;
    use app\models\User;
    use yii\helpers\ArrayHelper;
    use yii\rbac\Item;
    use yii\rbac\Rule;
    use Yii;

    class AccessToEndUserRule extends Rule
    {

        public $name = 'isCapableToAccessEndUser';

        /**
         * Executes the rule.
         *
         * @param string|integer $user the user ID. This should be either an integer or a string representing
         * the unique identifier of a user. See [[\yii\web\User::id]].
         * @param Item $item the role or permission that this rule is associated with
         * @param array $params parameters passed to [[ManagerInterface::checkAccess()]].
         * @return boolean a value indicating whether the rule permits the auth item it is associated with.
         */
        public function execute($user, $item, $params)
        {
            $role = array_keys(Yii::$app->authManager->getRolesByUser($user))[0]; //get role of user to which this rule is applied
            if ($role == User::ROLE_ENDY) {
                //endymed have access to all endusers
                return true;
            } elseif ($role == User::ROLE_SALES) {
                //sales have access to all end users
                return true;
            } elseif ($role == User::ROLE_DISTR) {
                //check do this end-user assigned to any from orders of this distributor

                //get distributor
                $distributor = Distributor::findOne(['user_id' => $user]);
                //get all orders of this distributor
                $purchaseOrders = $distributor->purchaseOrders;
                //get array of end-user id's assigned to orders
                $endUsers = [];
                foreach ($purchaseOrders as $purchaseOrder) {
                    $endUsers[] = $purchaseOrder->end_user_id;
                }

                //check if requested end user id is in the the array
                return in_array($params['modelId'], $endUsers);

            } else {
                return false;
            }
        }
    }<commit_msg>project-e-21 Error "Forbidden (#403)" when registration new end-users (distributor connection)<commit_after><?php
    /**
     * Created by PhpStorm.
     * User: rocks
     * Date: 9/5/14
     * Time: 1:44 PM
     */

    namespace app\rbac;


    use app\models\Distributor;
    use app\models\User;
    use yii\helpers\ArrayHelper;
    use yii\rbac\Item;
    use yii\rbac\Rule;
    use Yii;

    class AccessToEndUserRule extends Rule
    {

        public $name = 'isCapableToAccessEndUser';

        /**
         * Executes the rule.
         *
         * @param string|integer $user the user ID. This should be either an integer or a string representing
         * the unique identifier of a user. See [[\yii\web\User::id]].
         * @param Item $item the role or permission that this rule is associated with
         * @param array $params parameters passed to [[ManagerInterface::checkAccess()]].
         * @return boolean a value indicating whether the rule permits the auth item it is associated with.
         */
        public function execute($user, $item, $params)
        {
            $role = array_keys(Yii::$app->authManager->getRolesByUser($user))[0]; //get role of user to which this rule is applied
            if ($role == User::ROLE_ENDY) {
                //endymed have access to all endusers
                return true;
            } elseif ($role == User::ROLE_SALES) {
                //sales have access to all end users
                return true;
            } elseif ($role == User::ROLE_DISTR) {
                //return $this->checkIfEndUserRelatesToDistributorOrders($user, $params);
                return true;
            } else {
                return false;
            }
        }

        private function checkIfEndUserRelatesToDistributorOrders($user, $params)
        {
            //check do this end-user assigned to any from orders of this distributor

            //get distributor
            $distributor = Distributor::findOne(['user_id' => $user]);
            //get all orders of this distributor
            $purchaseOrders = $distributor->purchaseOrders;
            //get array of end-user id's assigned to orders
            $endUsers = [];
            foreach ($purchaseOrders as $purchaseOrder) {
                $endUsers[] = $purchaseOrder->end_user_id;
            }

            //check if requested end user id is in the the array
            return in_array($params['modelId'], $endUsers);
        }
    }
{'fraction_non_alphanumeric': 0.09765957446808511, 'fraction_numerical': 0.004468085106382979, 'mean_word_length': 1.8032200357781754, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2334325', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1241, 'n_words': 408}