ここの情報は古いです。ご理解頂いた上でお取り扱いください。

Changeset 10756


Ignore:
Timestamp:
Feb 13, 2009, 6:02:04 PM (13 years ago)
Author:
ShogoKawahara
Message:

#3628:fixed opJsonDbOpensocialService for the Persistence API

Location:
OpenPNE3/plugins/opOpenSocialPlugin/trunk
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • OpenPNE3/plugins/opOpenSocialPlugin/trunk/config/schema.yml

    r10308 r10756  
    4545  application_persistent_data:
    4646    id: ~
    47     member_application_id: { type: integer, foreignTable: member_application, foreignReference: id, onDelete: cascade }
    48     key: { type: varchar(128), required: true }
     47    application_id: { type: integer, foreignTable: application, foreignReference: id, onDelete: cascade }
     48    member_id: { type: integer, foreignTable: member, foreignReference: id, onDelete: cascade }
     49    name: { type: varchar(128), required: true }
    4950    value: { type : longvarchar, required: true }
    5051
  • OpenPNE3/plugins/opOpenSocialPlugin/trunk/lib/model/ApplicationPersistentDataPeer.php

    r10014 r10756  
    99 */
    1010
     11/**
     12 * ApplicationPersistentDataPeer
     13 *
     14 * @author Shogo Kawahara <kawahara@tejimaya.net>
     15 */
    1116class ApplicationPersistentDataPeer extends BaseApplicationPersistentDataPeer
    1217{
     18  public static function retrieveByApplicationIdAndMemberIdAndName($applicationId, $memberId, $name)
     19  {
     20    $criteria = new Criteria();
     21    $criteria->add(self::APPLICATION_ID, $applicationId);
     22    $criteria->add(self::MEMBER_ID, $memberId);
     23    $criteria->add(self::NAME, $name);
     24    return self::doSelectOne($criteria);
     25  }
     26
     27  public static function retrievesByApplicationIdAndMemberId($applicationId, $memberId, $fields = array())
     28  {
     29    $criteria = new Criteria();
     30    $criteria->add(self::APPLICATION_ID, $applicationId);
     31    $criteria->add(self::MEMBER_ID, $memberId);
     32    if (count($fields))
     33    {
     34      $criteria->add(self::NAME, $fields, Criteria::IN);
     35    }
     36    return self::doSelect($criteria);
     37  }
     38
     39  public static function getMemberFriendPersistentDatas($applicationId, $memberId, $fields = array())
     40  {
     41    $friendIds = MemberRelationshipPeer::getFriendMemberIds($memberId);
     42    $criteria = new Criteria();
     43    $criteria->add(self::APPLICATION_ID, $applicationId);
     44    $criteria->add(self::MEMBER_ID, $friendIds, Criteria::IN);
     45    if (count($fields))
     46    {
     47      $criteria->add(self::NAME, $fields, Criteria::IN);
     48    }
     49    return self::doSelect($criteria);
     50  }
    1351}
  • OpenPNE3/plugins/opOpenSocialPlugin/trunk/lib/user/opJsonDbOpensocialService.class.php

    r10546 r10756  
    11<?php
     2
     3/**
     4 * This file is part of the OpenPNE package.
     5 * (c) OpenPNE Project (http://www.openpne.jp/)
     6 *
     7 * For the full copyright and license information, please view the LICENSE
     8 * file and the NOTICE file that were distributed with this source code.
     9 */
    210
    311/**
     
    3543    $max   = $options->getCount();
    3644    $ret = array();
     45
    3746    $criteria = new Criteria();
    3847    $criteria->add(MemberPeer::ID, $ids, Criteria::IN);
    3948    $totalSize = MemberPeer::doCount($criteria);
     49
    4050    $criteria->addAscendingOrderByColumn(MemberPeer::ID);
    4151    if ($first !== false && $max !== false && is_numeric($first) && is_numeric($max) && $first >= 0 && $max > 0)
     
    4555    }
    4656    $members = MemberPeer::doSelect($criteria);
     57
    4758    $people = array();
    4859    foreach ($members as $member)
    4960    {
     61      //FIXME
    5062      $p = array();
    5163      $p['isOwner']  =  (!$token->isAnonymous() && $member->getId() == $token->getOwnerId()) ? true : false;
     
    7991      $people[] = $p;
    8092    }
     93
    8194    $collection = new RestfulCollection($people, $options->getStartIndex(), $totalSize);
    8295    $collection->setItemsPerPage($options->getCount());
     
    89102  }
    90103
    91   public function getActivity($userId, $groupId, $appdId, $fields, $activityId, SecurityToken $token)
     104  public function getActivity($userId, $groupId, $appId, $fields, $activityId, SecurityToken $token)
    92105  {
    93106    throw new SocialSpiException("Not implemented", ResponseError::$NOT_IMPLEMENTED);
     
    106119  public function getPersonData($userId, GroupId $groupId, $appId, $fields, SecurityToken $token)
    107120  {
    108     $ids = $this->getIdSet($userId, $groupId, $token);
    109     $criteria = new Criteria();
    110     $criteria->addJoin(ApplicationPersistentDataPeer::MEMBER_APPLICATION_ID, MemberApplicationPeer::ID);
    111     $criteria->add(MemberApplicationPeer::APPLICATION_ID, $appId);
    112     $criteria->add(MemberApplicationPeer::MEMBER_ID, $ids, Criteria::IN);
    113     if (count($fields))
    114     {
    115       $criteria->add(ApplicationPersistentDataPeer::KEY, $fields, Criteria::IN);
    116     } 
    117     $persistentDatas = ApplicationPersistentDataPeer::doSelect($criteria);
    118     if (!count($persistentDatas))
    119     {
    120       throw new SocialSpiException("UnKnown person app data key(s): ".implode(', ', $fields));
     121    if (!($userId instanceof UserId))
     122    {
     123      throw new SocialSpiException("Not support request", ResponseError::$NOT_IMPLEMENTED);
     124    }
     125    $targetUserId = (int)$userId->getUserId($token);
     126    $viewerId = (int)$token->getViewerId();
     127    if ($targetUserId != $viewerId)
     128    {
     129      $memberRelationship = MemberRelationshipPeer::retrieveByFromAndTo($targetUserId, $viewerId);
     130      if (!($memberRelationship && $memberRelationship->isFriend()))
     131      {
     132        throw new SocialSpiException("Unauthorized", ResponseError::$UNAUTHORIZED);
     133      }
     134    }
     135   if ($groupId->getType() == 'self')
     136    {
     137      $appPersistentDatas = ApplicationPersistentDataPeer::retrievesByApplicationIdAndMemberId($appId, $targetUserId, $fields);
     138    }
     139    else if($groupId->getType() == 'friends')
     140    {
     141      $appPersistentDatas = ApplicationPersistentDataPeer::getMemberFriendPersistentDatas($appId, $targetUserId, $fields); 
     142    }
     143    else
     144    {
     145      throw new SocialSpiException("We support getting data only when GROUP_ID is SELF or FRIENDS ", ResponseError::$NOT_IMPLEMENTED);
    121146    }
    122147    $data = array();
    123     foreach($persistentDatas as $persistentData)
    124     {
    125       $data[$persistentData->getMemberApplication()->getMemberId()][$persistentData->getKey()] = $persistentData->getValue();
     148    foreach ($appPersistentDatas as $appPersistentData)
     149    {
     150      $data[$appPersistentData->getMemberId()][$appPersistentData->getName()] = $appPersistentData->getValue();
    126151    }
    127152    return new DataCollection($data);
     
    130155  public function deletePersonData($userId, GroupId $groupId, $appId, $fields, SecurityToken $token)
    131156  {
     157    if (!($userId instanceof UserId) || $userId->getUserId($token) == null )
     158    {
     159      throw new SocialSpiException("Unknown person id", ResponseError::$NOT_FOUND);
     160    }
     161
     162    $targetUserId = $userId->getUserId($token);
     163   
     164    if ($targetUserId != $token->getViewerId())
     165    {
     166      throw new SocialSpiException("Unauthorized", ResponseError::$UNAUTHORIZED);
     167    }
     168   
    132169    foreach ($fields as $key)
    133170    {
     
    137174      }
    138175    }
    139     $ids = $this->getIdSet($userId, $groupId, $token);
    140     if (count($ids) < 1)
    141     {
    142       throw new InvalidArgumentException("No userId specified");
    143     }
    144     else if (count($ids) > 1)
    145     {
    146       throw new InvalidArgumentException("Multiple userIds not supported");
    147     }
    148     $userId = $ids[0];
    149     $appId = $token->getAppId();
     176
     177    $appPersistentDatas = ApplicationPersistentDataPeer::retrievesByApplicationIdAndMemberId($appId, $targetUserId, $fields);
     178   
     179    foreach ($appPersistentDatas as $appPersistentData)
     180    {
     181      $appPersistentData->delete();
     182    }
     183  }
     184
     185  public function updatePersonData(UserId $userId, GroupId $groupId, $appId, $fields, $values, SecurityToken $token)
     186  {
     187    if ($userId->getUserId($token) == null)
     188    {
     189      throw new SocialSpiException("Unknown person id", ResponseError::$NOT_FOUND);
     190    }
     191
    150192    foreach ($fields as $key)
    151193    {
    152       $criteria = new Criteria();
    153       $criteria->addJoin(ApplicationPersistentDataPeer::MEMBER_APPLICATION_ID, MemberApplicationPeer::ID);
    154       $criteria->add(MemberApplicationPeer::APPLICATION_ID, $appId);
    155       $criteria->add(MemberApplicationPeer::MEMBER_ID, $userId);
    156       $criteria->add(ApplicationPersistentDataPeer::KEY, $key);
    157       $persistentData = ApplicationPersistentDataPeer::doSelectOne($criteria);
    158       if (!$persistentData)
    159       {
    160         throw new SocialSpiException("Internal server error", ResponseError::$INTERNAL_ERROR);
    161       }
    162       $persistentData->delete();
    163     }
    164   }
    165 
    166   public function updatePersonData(UserId $userId, GroupId $groupId, $appId, $fields, $values, SecurityToken $token)
    167   {
    168     if ($userId->getUserId($token) == null)
    169     {
    170       throw new SocialSpiException("Unknown person id.", ResponseError::$NOT_FOUND);
    171     }
    172 
    173     foreach ($fields as $key)
    174     {
    175194      if (!preg_match('/[\w\-\.]+/',$key))
    176195      {
     
    178197      }
    179198    }
    180     switch ($groupId->getType())
    181     {
    182       case 'self':
    183         foreach ($fields as $key)
     199
     200    if ($groupId->getType() != 'self')
     201    {
     202      throw new SocialSpiException("We don't support updating data in batches yet", ResponseError::$NOT_IMPLEMENTED);
     203    }
     204
     205    $targetUserId = $userId->getUserId($token);
     206    if ($token->getOwnerId() == $targetUserId || $token->getViewerId() == $targetUserId)
     207    {
     208      $memberApplication = MemberApplicationPeer::retrieveByApplicationIdAndMemberId($appId, $targetUserId);
     209      if (!$memberApplication)
     210      {
     211        throw new SocialSpiException("Unauthorized", ResponseError::$UNAUTHORIZED);
     212      }
     213
     214      foreach($fields as $name)
     215      {
     216        $value = isset($values[$name]) ? $values[$name] : null;
     217        $appPersistentData = ApplicationPersistentDataPeer::retrieveByApplicationIdAndMemberIdAndName($appId, $targetUserId, $name);
     218        if (!$appPersistentData)
    184219        {
    185           $value = isset($values[$key]) ? $values[$key] : null;
    186           $criteria = new Criteria();
    187 
    188           $criteria->add(ApplicationPersistentDataPeer::MEMBER_APPLICATION_ID, $token->getModuleId());
    189           $criteria->add(ApplicationPersistentDataPeer::KEY, $key);
    190           $persistentData = ApplicationPersistentDataPeer::doSelectOne($criteria);
    191           if (!$persistentData)
    192           {
    193             $persistentData = new ApplicationPersistentData();
    194             $persistentData->setMemberApplicationId($token->getModuleId());
    195             $persistentData->setKey($key);
    196           }
    197           $persistentData->setValue($value);
    198           $persistentData->save();
     220          $appPersistentData = new ApplicationPersistentData();
     221          $appPersistentData->setApplicationId($appId);
     222          $appPersistentData->setMemberId($targetUserId);
     223          $appPersistentData->setName($name);
    199224        }
    200         break;
    201       default:
    202         throw new SocialSpiException("We don't support updating data in batches yet", ResponseError::$NOT_IMPLEMENTED);
     225        $appPersistentData->setValue($value);
     226        $appPersistentData->save();
     227      }
     228    }
     229    else
     230    {
     231      throw new SocialSpiException("Unauthorized", ResponseError::$UNAUTHORIZED);
    203232    }
    204233  }
     
    209238  }
    210239
    211   private function getIdSet($user, GroupId $group, SecurityToken $token)
     240  protected function getIdSet($user, GroupId $group, SecurityToken $token)
    212241  {
    213242    $ids = array();
     
    224253        case 'friends':
    225254        case 'groupId':
    226 
    227           $criteria = new Criteria();
    228           $criteria->add(MemberRelationshipPeer::MEMBER_ID_FROM,$userId);
    229           $criteria->add(MemberRelationshipPeer::IS_FRIEND, true);
    230           $friends = MemberRelationshipPeer::doSelect($criteria);
    231          
    232           $friendIds = array();
    233           foreach ($friends as $friend)
    234           {
    235             $friendIds[] = $friend->getMemberIdTo();
    236           }
    237           if (count($friendIds))
    238           {
    239             $ids = $friendIds;
    240           }
     255          $ids = MemberRelationshipPeer::getFriendMemberIds($userId);
    241256          break;
    242257        case 'self':
Note: See TracChangeset for help on using the changeset viewer.