Ejemplo n.º 1
0
 public function testValidateNoLogWrongBadge()
 {
     $badge = new Badge();
     $badge->setId(rand(PHP_INT_MAX / 2 + 1, PHP_INT_MAX));
     $rule = new BadgeRule();
     $rule->setBadge($badge);
     $log = new Log();
     $log->setDetails(array('badge' => array('id' => rand(0, PHP_INT_MAX / 2))));
     $badgeConstraint = new BadgeConstraint();
     $badgeConstraint->setRule($rule)->setAssociatedLogs(array($log));
     $this->assertFalse($badgeConstraint->validate());
 }
Ejemplo n.º 2
0
 private function createLog(LogGenericEvent $event)
 {
     //Add doer details
     $doer = null;
     $doerIp = null;
     $doerSessionId = null;
     $doerType = null;
     //Event can override the doer
     if ($event->getDoer() === null) {
         $token = $this->tokenStorage->getToken();
         if ($token === null) {
             $doer = null;
             $doerType = Log::doerTypePlatform;
         } else {
             if ($token->getUser() === 'anon.') {
                 $doer = null;
                 $doerType = Log::doerTypeAnonymous;
             } else {
                 $doer = $token->getUser();
                 $doerType = Log::doerTypeUser;
             }
             if ($this->container->isScopeActive('request')) {
                 $request = $this->container->get('request');
                 $doerSessionId = $request->getSession()->getId();
                 $doerIp = $request->getClientIp();
             } else {
                 $doerIp = 'CLI';
             }
         }
     } elseif (LogGenericEvent::PLATFORM_EVENT_TYPE === $event->getDoer()) {
         $doer = null;
         $doerType = Log::doerTypePlatform;
     } else {
         $doer = $event->getDoer();
         $doerType = Log::doerTypeUser;
     }
     $log = new Log();
     //Simple type properties
     $log->setAction($event->getAction())->setToolName($event->getToolName())->setIsDisplayedInAdmin($event->getIsDisplayedInAdmin())->setIsDisplayedInWorkspace($event->getIsDisplayedInWorkspace())->setOtherElementId($event->getOtherElementId());
     //Object properties
     $log->setOwner($event->getOwner());
     if (!($event->getAction() === LogUserDeleteEvent::ACTION && $event->getReceiver() === $doer)) {
         //Prevent self delete case
         //Sometimes, the entity manager has been cleared, so we must merge the doer.
         if ($doer) {
             $doer = $this->om->merge($doer);
         }
         $log->setDoer($doer);
     }
     $log->setDoerType($doerType);
     $log->setDoerIp($doerIp);
     $log->setDoerSessionId($doerSessionId);
     if ($event->getAction() !== LogUserDeleteEvent::ACTION) {
         //Prevent user delete case
         $log->setReceiver($event->getReceiver());
     }
     if ($event->getAction() !== LogGroupDeleteEvent::ACTION) {
         if ($receiverGroup = $event->getReceiverGroup()) {
             $this->om->merge($receiverGroup);
         }
         $log->setReceiverGroup($receiverGroup);
     }
     if (!($event->getAction() === LogResourceDeleteEvent::ACTION && $event->getResource() === $event->getWorkspace())) {
         //Prevent delete workspace case
         $log->setWorkspace($event->getWorkspace());
     }
     if ($event->getAction() !== LogResourceDeleteEvent::ACTION) {
         //Prevent delete resource case
         $log->setResourceNode($event->getResource());
     }
     if ($event->getAction() !== LogWorkspaceRoleDeleteEvent::ACTION) {
         //Prevent delete role case
         $log->setRole($event->getRole());
     }
     if ($doer !== null) {
         $platformRoles = $this->roleManager->getPlatformRoles($doer);
         foreach ($platformRoles as $platformRole) {
             $log->addDoerPlatformRole($platformRole);
         }
         if ($event->getWorkspace() !== null) {
             $workspaceRoles = $this->roleManager->getWorkspaceRolesForUser($doer, $event->getWorkspace());
             foreach ($workspaceRoles as $workspaceRole) {
                 $log->addDoerWorkspaceRole($workspaceRole);
             }
         }
     }
     if ($event->getResource() !== null) {
         $log->setResourceType($event->getResource()->getResourceType());
     }
     //Json_array properties
     $details = $event->getDetails();
     if ($details === null) {
         $details = array();
     }
     if ($doer !== null) {
         $details['doer'] = array('firstName' => $doer->getFirstName(), 'lastName' => $doer->getLastName(), 'publicUrl' => $doer->getPublicUrl());
         if (count($log->getDoerPlatformRoles()) > 0) {
             $doerPlatformRolesDetails = array();
             foreach ($log->getDoerPlatformRoles() as $platformRole) {
                 $doerPlatformRolesDetails[] = $platformRole->getTranslationKey();
             }
             $details['doer']['platformRoles'] = $doerPlatformRolesDetails;
         }
         if (count($log->getDoerWorkspaceRoles()) > 0) {
             $doerWorkspaceRolesDetails = array();
             foreach ($log->getDoerWorkspaceRoles() as $workspaceRole) {
                 $doerWorkspaceRolesDetails[] = $workspaceRole->getTranslationKey();
             }
             $details['doer']['workspaceRoles'] = $doerWorkspaceRolesDetails;
         }
     }
     $log->setDetails($details);
     $this->om->persist($log);
     $this->om->flush();
     $createLogEvent = new LogCreateEvent($log);
     $this->container->get('event_dispatcher')->dispatch(LogCreateEvent::NAME, $createLogEvent);
 }
Ejemplo n.º 3
0
 public function doerProvider()
 {
     $log = new Log();
     $action = uniqid();
     $user = new User();
     $rule = new BadgeRule();
     $rule->setAction($action)->setUser($user)->setUserType(0);
     $badge = new Badge();
     $rule2 = new BadgeRule();
     $rule2->setAction($action)->setUser($user)->setUserType(0)->setBadge($badge);
     $log2 = new Log();
     $log2->setDetails(array('badge' => array('id' => $badge->getId())));
     $badge2 = new Badge();
     $badge2->setId(rand(PHP_INT_MAX / 2 + 1, PHP_INT_MAX));
     $rule3 = new BadgeRule();
     $rule3->setAction($action)->setUser($user)->setUserType(0)->setBadge($badge2);
     $log3 = new Log();
     $log3->setDetails(array('badge' => array('id' => rand(0, PHP_INT_MAX / 2))));
     $rule4 = new BadgeRule();
     $rule4->setAction($action)->setUser($user)->setUserType(0)->setResult(rand(0, PHP_INT_MAX))->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_EQUAL));
     $result = rand(0, PHP_INT_MAX);
     $log4 = new Log();
     $log4->setDetails(array('result' => $result));
     $rule5 = new BadgeRule();
     $rule5->setAction($action)->setUser($user)->setUserType(0)->setResult($result - 1)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_EQUAL));
     $rule6 = new BadgeRule();
     $rule6->setAction($action)->setUser($user)->setUserType(0)->setResult($result)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_EQUAL));
     $log5 = new Log();
     $log5->setDetails(array('result' => 12));
     $rule7 = new BadgeRule();
     $rule7->setAction($action)->setUser($user)->setUserType(0)->setResult(9)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR));
     $rule8 = new BadgeRule();
     $rule8->setAction($action)->setUser($user)->setUserType(0)->setResult(42)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR));
     $rule9 = new BadgeRule();
     $rule9->setAction($action)->setUser($user)->setUserType(0)->setResult(12)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR));
     $rule10 = new BadgeRule();
     $rule10->setAction($action)->setUser($user)->setUserType(0)->setResult(9)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR_EQUAL));
     $rule11 = new BadgeRule();
     $rule11->setAction($action)->setUser($user)->setUserType(0)->setResult(12)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR_EQUAL));
     $log6 = new Log();
     $log6->setDetails(array('result' => 9));
     $rule12 = new BadgeRule();
     $rule12->setAction($action)->setUser($user)->setUserType(0)->setResult(12)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR_EQUAL));
     $rule13 = new BadgeRule();
     $rule13->setAction($action)->setUser($user)->setUserType(0)->setResult(12)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_INFERIOR));
     $log7 = new Log();
     $log7->setDetails(array('result' => 42));
     $rule14 = new BadgeRule();
     $rule14->setAction($action)->setUser($user)->setUserType(0)->setResult(12)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_INFERIOR_EQUAL));
     $rule15 = new BadgeRule();
     $rule15->setAction($action)->setUser($user)->setUserType(0)->setResult(12)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_SUPERIOR_EQUAL));
     $rule16 = new BadgeRule();
     $rule16->setAction($action)->setUser($user)->setUserType(0)->setResult(9)->setResultComparison(Rule::getResultComparisonTypeValue(Rule::RESULT_INFERIOR_EQUAL));
     return array(array($user, $action, $rule, array(), false), array($user, $action, $rule, array($log), array($log)), array($user, $action, $rule2, array($log2), array($log2)), array($user, $action, $rule2, array(), false), array($user, $action, $rule3, array($log3), false), array($user, $action, $rule4, array(), false), array($user, $action, $rule5, array($log4), false), array($user, $action, $rule6, array($log4), array($log4)), array($user, $action, $rule7, array($log5), array($log5)), array($user, $action, $rule8, array($log5), false), array($user, $action, $rule9, array($log5), false), array($user, $action, $rule10, array($log5), array($log5)), array($user, $action, $rule11, array($log5), array($log5)), array($user, $action, $rule12, array($log6), false), array($user, $action, $rule13, array($log6), array($log6)), array($user, $action, $rule13, array($log7), false), array($user, $action, $rule13, array($log5), false), array($user, $action, $rule14, array($log6), array($log6)), array($user, $action, $rule15, array($log5), array($log5)), array($user, $action, $rule16, array($log5), false));
 }
Ejemplo n.º 4
0
 public function testValidateTwoRightOneWrongLogLogResultSuperiorEqual()
 {
     $result = rand(0, PHP_INT_MAX / 2);
     $badgeRule = new BadgeRule();
     $badgeRule->setOccurrence(1)->setResult($result)->setResultComparison(4);
     $log = new Log();
     $log->setDetails(array('result' => $result + rand(0, PHP_INT_MAX / 2)));
     $log2 = new Log();
     $log2->setDetails(array('result' => $result - 1));
     $log3 = new Log();
     $log3->setDetails(array('result' => $result + rand(0, PHP_INT_MAX / 2)));
     $associatedLogs = array($log, $log2, $log3);
     $resultConstraint = new ResultConstraint();
     $resultConstraint->setRule($badgeRule)->setAssociatedLogs($associatedLogs);
     $this->assertTrue($resultConstraint->validate());
 }