Beispiel #1
0
 /**
  * @param array $params
  * @throws Exception
  */
 public function authenticate(array $params)
 {
     if (!isset($params['authtoken']) || empty($params['authtoken'])) {
         return new Response(Http::STATUS_FORBIDDEN, 'Invalid or empty authToken');
     }
     $authToken = ApiAuthenticationService::instance()->getAuthToken($params['authtoken']);
     if (empty($authToken)) {
         return new Response(Http::STATUS_FORBIDDEN, 'Auth token not found');
     }
     $user = UserService::instance()->getUserById($authToken['userId']);
     if (empty($user)) {
         return new Response(Http::STATUS_FORBIDDEN, 'User not found');
     }
     $credentials = new SessionCredentials($user);
     $credentials->setAuthProvider('API');
     $credentials->addRoles(UserRole::USER);
     $credentials->addFeatures(UserFeaturesService::instance()->getUserFeatures($authToken['userId']));
     $credentials->addRoles(UserService::instance()->getUserRolesByUserId($authToken['userId']));
     $subscription = SubscriptionsService::instance()->getUserActiveSubscription($authToken['userId']);
     if (!empty($subscription)) {
         $credentials->addRoles(UserRole::SUBSCRIBER);
         $credentials->addFeatures(UserFeature::SUBSCRIBER);
         if ($subscription['subscriptionTier'] == 2) {
             $credentials->addFeatures(UserFeature::SUBSCRIBERT2);
         }
         if ($subscription['subscriptionTier'] == 3) {
             $credentials->addFeatures(UserFeature::SUBSCRIBERT3);
         }
     }
     $response = new Response(Http::STATUS_OK, json_encode($credentials->getData()));
     $response->addHeader(Http::HEADER_CONTENTTYPE, MimeType::JSON);
     return $response;
 }
Beispiel #2
0
 /**
  * @Route ("/api/messages/send")
  * @HttpMethod ({"POST"})
  *
  * Expects the following GET|POST variables:
  *     privatekey=XXXXXXXX
  *     message=string
  *     userid=999
  *     targetuserid=999
  *
  * @param array $params
  * @return Response
  */
 public function sendMessage(array $params)
 {
     $privateMessageService = PrivateMessageService::instance();
     $chatIntegrationService = ChatIntegrationService::instance();
     $userService = UserService::instance();
     $response = array();
     try {
         FilterParams::required($params, 'privatekey');
         FilterParams::required($params, 'message');
         FilterParams::required($params, 'userid');
         FilterParams::required($params, 'targetuserid');
         if (!$this->checkPrivateKey($params['privatekey'])) {
             throw new Exception('Invalid shared private key.');
         }
         if ($params['userid'] == $params['targetuserid']) {
             throw new Exception('Cannot send messages to yourself.');
         }
         $ban = $userService->getUserActiveBan($params['userid']);
         if (!empty($ban)) {
             throw new Exception("privmsgbanned");
         }
         $oldEnough = $userService->isUserOldEnough($params['userid']);
         if (!$oldEnough) {
             throw new Exception("privmsgaccounttooyoung");
         }
         $user = $userService->getUserById($params['userid']);
         $credentials = new SessionCredentials($user);
         $credentials->addRoles($userService->getUserRolesByUserId($params['userid']));
         $targetuser = $userService->getUserById($params['targetuserid']);
         if (empty($targetuser)) {
             throw new Exception('notfound');
         }
         $canSend = $privateMessageService->canSend($credentials, $params['targetuserid']);
         if (!$canSend) {
             throw new Exception("throttled");
         }
         if (empty($user)) {
             throw new Exception('notfound');
         }
         $message = array('userid' => $params['userid'], 'targetuserid' => $params['targetuserid'], 'message' => $params['message'], 'isread' => 0);
         $message['id'] = $privateMessageService->addMessage($message);
         $chatIntegrationService->publishPrivateMessage(array('messageid' => $message['id'], 'message' => $message['message'], 'username' => $user['username'], 'userid' => $user['userId'], 'targetusername' => $targetuser['username'], 'targetuserid' => $targetuser['userId']));
         $response = new Response(Http::STATUS_NO_CONTENT);
     } catch (Exception $e) {
         $response['success'] = false;
         $response['error'] = $e->getMessage();
         $response = new Response(Http::STATUS_BAD_REQUEST, json_encode($response));
         $response->addHeader(Http::HEADER_CONTENTTYPE, MimeType::JSON);
     }
     return $response;
 }
 /**
  * @param array $user
  * @param string $authProvider
  * @return SessionCredentials
  */
 public function getUserCredentials(array $user, $authProvider)
 {
     $credentials = new SessionCredentials($user);
     $credentials->setAuthProvider($authProvider);
     $credentials->addRoles(UserRole::USER);
     $credentials->addFeatures(UserFeaturesService::instance()->getUserFeatures($user['userId']));
     $credentials->addRoles(UserService::instance()->getUserRolesByUserId($user['userId']));
     $subscription = SubscriptionsService::instance()->getUserActiveSubscription($user['userId']);
     if (!empty($subscription) or $user['istwitchsubscriber']) {
         $credentials->addRoles(UserRole::SUBSCRIBER);
         $credentials->addFeatures(UserFeature::SUBSCRIBER);
         if ($user['istwitchsubscriber']) {
             $credentials->addFeatures(UserFeature::SUBSCRIBERT0);
         }
     }
     if (!empty($subscription)) {
         if ($subscription['subscriptionTier'] == 2) {
             $credentials->addFeatures(UserFeature::SUBSCRIBERT2);
         }
         if ($subscription['subscriptionTier'] == 3) {
             $credentials->addFeatures(UserFeature::SUBSCRIBERT3);
         }
         if ($subscription['subscriptionTier'] == 4) {
             $credentials->addFeatures(UserFeature::SUBSCRIBERT4);
         }
     }
     return $credentials;
 }