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
9 final class PhortuneAccount
extends PhortuneDAO
11 PhabricatorApplicationTransactionInterface
,
12 PhabricatorPolicyInterface
{
15 protected $billingName;
16 protected $billingAddress;
18 private $memberPHIDs = self
::ATTACHABLE
;
19 private $merchantPHIDs = self
::ATTACHABLE
;
21 public static function initializeNewAccount(PhabricatorUser
$actor) {
24 ->setBillingAddress('')
25 ->attachMerchantPHIDs(array())
26 ->attachMemberPHIDs(array());
29 public static function createNewAccount(
30 PhabricatorUser
$actor,
31 PhabricatorContentSource
$content_source) {
33 $account = self
::initializeNewAccount($actor);
36 $xactions[] = id(new PhortuneAccountTransaction())
37 ->setTransactionType(PhortuneAccountNameTransaction
::TRANSACTIONTYPE
)
38 ->setNewValue(pht('Default Account'));
40 $xactions[] = id(new PhortuneAccountTransaction())
41 ->setTransactionType(PhabricatorTransactions
::TYPE_EDGE
)
44 PhortuneAccountHasMemberEdgeType
::EDGECONST
)
47 '=' => array($actor->getPHID() => $actor->getPHID()),
50 $editor = id(new PhortuneAccountEditor())
52 ->setContentSource($content_source);
54 // We create an account for you the first time you visit Phortune.
55 $unguarded = AphrontWriteGuard
::beginScopedUnguardedWrites();
57 $editor->applyTransactions($account, $xactions);
64 public function newCart(
65 PhabricatorUser
$actor,
66 PhortuneCartImplementation
$implementation,
67 PhortuneMerchant
$merchant) {
69 $cart = PhortuneCart
::initializeNewCart($actor, $this, $merchant);
71 $cart->setCartClass(get_class($implementation));
72 $cart->attachImplementation($implementation);
74 $implementation->willCreateCart($actor, $cart);
79 protected function getConfiguration() {
81 self
::CONFIG_AUX_PHID
=> true,
82 self
::CONFIG_COLUMN_SCHEMA
=> array(
84 'billingName' => 'text255',
85 'billingAddress' => 'text',
87 ) + parent
::getConfiguration();
90 public function generatePHID() {
91 return PhabricatorPHID
::generateNewPHID(
92 PhortuneAccountPHIDType
::TYPECONST
);
95 public function getMemberPHIDs() {
96 return $this->assertAttached($this->memberPHIDs
);
99 public function attachMemberPHIDs(array $phids) {
100 $this->memberPHIDs
= $phids;
104 public function getURI() {
106 '/phortune/account/%d/',
110 public function getDetailsURI() {
112 '/phortune/account/%d/details/',
116 public function getOrdersURI() {
118 '/phortune/account/%d/orders/',
122 public function getOrderListURI($path = '') {
124 '/phortune/account/%d/orders/list/%s',
129 public function getSubscriptionsURI() {
131 '/phortune/account/%d/subscriptions/',
135 public function getEmailAddressesURI() {
137 '/phortune/account/%d/addresses/',
141 public function getPaymentMethodsURI() {
143 '/phortune/account/%d/methods/',
147 public function getChargesURI() {
149 '/phortune/account/%d/charges/',
153 public function getChargeListURI($path = '') {
155 '/phortune/account/%d/charges/list/%s',
160 public function attachMerchantPHIDs(array $merchant_phids) {
161 $this->merchantPHIDs
= $merchant_phids;
165 public function getMerchantPHIDs() {
166 return $this->assertAttached($this->merchantPHIDs
);
169 public function writeMerchantEdge(PhortuneMerchant
$merchant) {
170 $edge_src = $this->getPHID();
171 $edge_type = PhortuneAccountHasMerchantEdgeType
::EDGECONST
;
172 $edge_dst = $merchant->getPHID();
174 id(new PhabricatorEdgeEditor())
175 ->addEdge($edge_src, $edge_type, $edge_dst)
181 public function isUserAccountMember(PhabricatorUser
$user) {
182 $user_phid = $user->getPHID();
187 $member_map = array_fuse($this->getMemberPHIDs());
189 return isset($member_map[$user_phid]);
192 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
195 public function getApplicationTransactionEditor() {
196 return new PhortuneAccountEditor();
199 public function getApplicationTransactionTemplate() {
200 return new PhortuneAccountTransaction();
204 /* -( PhabricatorPolicyInterface )----------------------------------------- */
207 public function getCapabilities() {
209 PhabricatorPolicyCapability
::CAN_VIEW
,
210 PhabricatorPolicyCapability
::CAN_EDIT
,
214 public function getPolicy($capability) {
215 switch ($capability) {
216 case PhabricatorPolicyCapability
::CAN_VIEW
:
217 case PhabricatorPolicyCapability
::CAN_EDIT
:
218 if ($this->getPHID() === null) {
219 // Allow a user to create an account for themselves.
220 return PhabricatorPolicies
::POLICY_USER
;
222 return PhabricatorPolicies
::POLICY_NOONE
;
227 public function hasAutomaticCapability($capability, PhabricatorUser
$viewer) {
228 if ($this->isUserAccountMember($viewer)) {
232 // See T13366. If the viewer can edit any merchant that this payment
233 // account has a relationship with, they can see the payment account.
234 if ($capability == PhabricatorPolicyCapability
::CAN_VIEW
) {
235 $viewer_phids = array($viewer->getPHID());
236 $merchant_phids = $this->getMerchantPHIDs();
238 $any_edit = PhortuneMerchantQuery
::canViewersEditMerchants(
250 public function describeAutomaticCapability($capability) {
252 pht('Members of an account can always view and edit it.'),
253 pht('Merchants an account has established a relationship can view it.'),