[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * An account represents a purchasing entity. An account may have multiple users 5 * on it (e.g., several employees of a company have access to the company 6 * account), and a user may have several accounts (e.g., a company account and 7 * a personal account). 8 */ 9 final class PhortuneAccount extends PhortuneDAO 10 implements PhabricatorPolicyInterface { 11 12 protected $name; 13 14 private $memberPHIDs = self::ATTACHABLE; 15 16 public static function initializeNewAccount(PhabricatorUser $actor) { 17 $account = id(new PhortuneAccount()); 18 19 $account->memberPHIDs = array(); 20 21 return $account; 22 } 23 24 public static function createNewAccount( 25 PhabricatorUser $actor, 26 PhabricatorContentSource $content_source) { 27 28 $account = PhortuneAccount::initializeNewAccount($actor); 29 30 $xactions = array(); 31 $xactions[] = id(new PhortuneAccountTransaction()) 32 ->setTransactionType(PhortuneAccountTransaction::TYPE_NAME) 33 ->setNewValue(pht('Personal Account')); 34 35 $xactions[] = id(new PhortuneAccountTransaction()) 36 ->setTransactionType(PhabricatorTransactions::TYPE_EDGE) 37 ->setMetadataValue( 38 'edge:type', 39 PhortuneAccountHasMemberEdgeType::EDGECONST) 40 ->setNewValue( 41 array( 42 '=' => array($actor->getPHID() => $actor->getPHID()), 43 )); 44 45 $editor = id(new PhortuneAccountEditor()) 46 ->setActor($actor) 47 ->setContentSource($content_source); 48 49 // We create an account for you the first time you visit Phortune. 50 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 51 52 $editor->applyTransactions($account, $xactions); 53 54 unset($unguarded); 55 56 return $account; 57 } 58 59 public function newCart( 60 PhabricatorUser $actor, 61 PhortuneCartImplementation $implementation, 62 PhortuneMerchant $merchant) { 63 64 $cart = PhortuneCart::initializeNewCart($actor, $this, $merchant); 65 66 $cart->setCartClass(get_class($implementation)); 67 $cart->attachImplementation($implementation); 68 69 $implementation->willCreateCart($actor, $cart); 70 71 return $cart->save(); 72 } 73 74 public function getConfiguration() { 75 return array( 76 self::CONFIG_AUX_PHID => true, 77 self::CONFIG_COLUMN_SCHEMA => array( 78 'name' => 'text255', 79 ), 80 ) + parent::getConfiguration(); 81 } 82 83 public function generatePHID() { 84 return PhabricatorPHID::generateNewPHID( 85 PhortuneAccountPHIDType::TYPECONST); 86 } 87 88 public function getMemberPHIDs() { 89 return $this->assertAttached($this->memberPHIDs); 90 } 91 92 public function attachMemberPHIDs(array $phids) { 93 $this->memberPHIDs = $phids; 94 return $this; 95 } 96 97 98 /* -( PhabricatorPolicyInterface )----------------------------------------- */ 99 100 101 public function getCapabilities() { 102 return array( 103 PhabricatorPolicyCapability::CAN_VIEW, 104 PhabricatorPolicyCapability::CAN_EDIT, 105 ); 106 } 107 108 public function getPolicy($capability) { 109 switch ($capability) { 110 case PhabricatorPolicyCapability::CAN_VIEW: 111 // Accounts are technically visible to all users, because merchant 112 // controllers need to be able to see accounts in order to process 113 // orders. We lock things down more tightly at the application level. 114 return PhabricatorPolicies::POLICY_USER; 115 case PhabricatorPolicyCapability::CAN_EDIT: 116 if ($this->getPHID() === null) { 117 // Allow a user to create an account for themselves. 118 return PhabricatorPolicies::POLICY_USER; 119 } else { 120 return PhabricatorPolicies::POLICY_NOONE; 121 } 122 } 123 } 124 125 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { 126 $members = array_fuse($this->getMemberPHIDs()); 127 return isset($members[$viewer->getPHID()]); 128 } 129 130 public function describeAutomaticCapability($capability) { 131 return pht('Members of an account can always view and edit it.'); 132 } 133 134 135 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |