Add an Attribution

@param int $sourceId
@param int $targetId
@param int $roleId

@return Attribution|null
556792
public function add($sourceId, $targetId, $roleId)
    {
        $request = (new RequestDescriptor())
            ->setUrl($this->buildUrl(self::IDP_ATTRIBUTIONS_API))
            ->setMethod('POST');

        $request->setBodyParams([
            'data' => json_encode([
                'source_id' => $sourceId,
                'target_id' => $targetId,
                'role_id' => $roleId,
            ])
        ]);

        try {
            $response = json_decode($this->send($request)->getBody(), true);
            return (count($response['added']) > 0 ? reset($response['added']) : null);
        } catch (\Exception $e) {
            $previous = $e->getPrevious();

            if ($previous instanceof BadResponseException) {
                $error = json_decode($previous->getResponse()->getBody(), true);

                throw new UserAttributionException($error['error'], $error['code'], $previous);
            }

            throw new UserAttributionException($e->getMessage(), $e->getCode(), $e->getPrevious());
        }
    }
801704
public function addPermissions($roles, $resourceId)
    {
        if (empty($roles)) {
            return;
        }

        $allRoles = $this->aclDataService->getAllRoles()->getData();

        // If all roles are selected, then no roles should be set (all roles allowed)
        // This assumes that the current rules have been deleted or they are empty
        if (count($roles) == count($allRoles)) {
            return $allRoles;
        }

        foreach ($roles as $role) {
            $this->addPermission($role['roleId'], $resourceId);
        }

        if (count($roles) > 0) {
            $this->aclDataService->createRule(
                $this->getAclRule('guest', $resourceId, 'deny')
            );
        }

        return $roles;
    }
1227215
public function loadPolicy($roleId, $policyId, Request $request)
    {
        $loadedRole = $this->roleService->loadRole($roleId);
        foreach ($loadedRole->getPolicies() as $policy) {
            if ($policy->id == $policyId) {
                return $policy;
            }
        }

        throw new Exceptions\NotFoundException("Policy not found: '{$request->getPathInfo()}'.");
    }
133399
protected function getAclRule($roleId, $resourceId, $allowDeny = 'allow')
    {
        $rule = new AclRule();
        $rule->setRoleId($roleId);
        $rule->setRule($allowDeny);
        $rule->setResourceId($resourceId);
        $rule->setPrivilege('read');

        return $rule;
    }
891222
public function loadRoleAssignmentsByRoleId($roleId)
    {
        $data = $this->roleGateway->loadRoleAssignmentsByRoleId($roleId);

        if (empty($data)) {
            return array();
        }

        return $this->mapper->mapRoleAssignments($data);
    }
58057
public function addLead($labelId, $leadId)
    {
        $response = giga_facebook_post($labelId . '/label', [
            'user' => $leadId
        ]);

        return isset($response->success);
    }
78737
public function removeRole($roleId, $pageId = 0)
    {
        $this->roles()
            ->wherePivot(self::PIVOT_ATTR_ROLE_ID, '=', $roleId)
            ->wherePivot(self::PIVOT_ATTR_PAGE_ID, '=', $pageId)
            ->detach();

        return $this;
    }
1172402
public function upsertOne(
        int $sourceId,
        string $name,
        $value,
        $type = null
    ) : array {
        if ($type === null) {
            $type = $this->typeInfer($value);
        }

        return $this->sendPut(
            sprintf('/profiles/%s/features', $this->userName),
            [],
            [
                'source_id' => $sourceId,
                'name'      => $name,
                'value'     => $value,
                'type'      => $type
            ]
        );
    }
1147261
public function get($sourceId, $targetId = null)
    {
        $url = self::IDP_ATTRIBUTIONS_API . '/' . $sourceId;
        if ($targetId) {
            $url .= '?target=' . urlencode($targetId);
        }

        $request = (new RequestDescriptor())
            ->setUrl($this->buildUrl($url))
            ->setMethod('GET');

        try {
            return json_decode($this->send($request)->getBody(), true);
        } catch (\Exception $e) {
            $previous = $e->getPrevious();

            if ($previous instanceof BadResponseException) {
                $error = json_decode($previous->getResponse()->getBody(), true);

                throw new UserAttributionException($error['error'], $error['code'], $previous);
            }

            throw new UserAttributionException($e->getMessage(), $e->getCode(), $e->getPrevious());
        }
    }
1205680
public function execute($sourceId = null, $sourceSurveyId = null, $surveyId = null)
    {
        $batch = $this->getBatch();

        if ($surveyId) {
            $survey = $this->loader->getTracker()->getSurvey($surveyId);
        } else {
            $survey = $this->loader->getTracker()->getSurveyBySourceId($sourceSurveyId, $sourceId);
        }

        if (! $survey->isActive()) {
            return;
        }

        // Now save the questions
        $answerModel = $survey->getAnswerModel('en');
        
        $hash = $survey->calculateHash();
        
        if ($survey->getHash() === $hash) {
            return;
        }
        
        $survey->setHash($hash, $this->loader->getCurrentUser()->getUserId());

        foreach ($answerModel->getItemsOrdered() as $order => $name) {
            if (true === $answerModel->get($name, 'survey_question')) {
                $batch->addTask('Tracker_RefreshQuestion', $surveyId, $name, $order);
            }
        }

        // If we have a response database, create a view on the answers
        if ($this->project->hasResponseDatabase()) {
            $this->replaceCreateView($survey, $answerModel);
        }
    }
346563
public function createNew(
        int $sourceId,
        string $collectionName,
        array $data
    ) : array {
        return $this->sendPost(
            sprintf('/profiles/%s/raw', $this->userName),
            [],
            [
                'source_id'  => $sourceId,
                'collection' => $collectionName,
                'data'       => $data
            ]
        );
    }
875756
public function unassignRoleFromUser($userId, $roleId)
    {
        $user = $this->userService->loadUser($userId);
        $role = $this->roleService->loadRole($roleId);

        $this->roleService->unassignRoleFromUser($role, $user);

        $roleAssignments = $this->roleService->getRoleAssignmentsForUser($user);

        return new Values\RoleAssignmentList($roleAssignments, $user->id);
    }
846624
public function addManyToOne($name, $targetEntity, $inversedBy = null)
    {
        $builder = $this->createManyToOne($name, $targetEntity);

        if ($inversedBy) {
            $builder->inversedBy($inversedBy);
        }

        return $builder->build();
    }
657060
public function addPolicy($roleId, Policy $policy)
    {
        $legacyPolicy = clone $policy;
        $this->limitationConverter->toLegacy($legacyPolicy);

        $this->roleGateway->addPolicy($roleId, $legacyPolicy);
        $policy->id = $legacyPolicy->id;
        $policy->roleId = $legacyPolicy->roleId;

        return $policy;
    }
250368
protected function insert(Source $invoiceSource, $entryId, $token, $created)
    {
        /** @noinspection PhpUnhandledExceptionInspection */
        $record = $this
            ->getModel()
            ->setEntryId($entryId)
            ->setToken($token)
            ->setSourceType($invoiceSource->getType())
            ->setSourceId($invoiceSource->getId())
            ->setUpdated($created
            );
        return $this->getResourceModel()->save($record);
    }
904402
public function updateOne(int $sourceId, array $tags, int $otpCode = null, string $ipaddr = '') : array {
        $array = [
            'tags' => $tags
        ];

        if ($otpCode !== null) {
            $array['otpCode'] = $otpCode;
        }

        return $this->sendPatch(
            sprintf('/profiles/%s/sources/%s', $this->userName, $sourceId),
            [],
            $array
        );
    }
658856
public function createTarget($targetId, $base)
    {

        if(!is_string($base) && !is_array($base)){
            throw new \common_Exception('Invalid base format');
        }

        $array = array(
            'base'  => $base,
            'available' => array()
        );
        $this->set($targetId, $array);
    }