Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / phortune / storage / PhortuneAccount.php
blobc823b6a8113624350af3e5f2c82efbd0c665aeb0
1 <?php
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
11 PhabricatorApplicationTransactionInterface,
12 PhabricatorPolicyInterface {
14 protected $name;
15 protected $billingName;
16 protected $billingAddress;
18 private $memberPHIDs = self::ATTACHABLE;
19 private $merchantPHIDs = self::ATTACHABLE;
21 public static function initializeNewAccount(PhabricatorUser $actor) {
22 return id(new self())
23 ->setBillingName('')
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);
35 $xactions = array();
36 $xactions[] = id(new PhortuneAccountTransaction())
37 ->setTransactionType(PhortuneAccountNameTransaction::TRANSACTIONTYPE)
38 ->setNewValue(pht('Default Account'));
40 $xactions[] = id(new PhortuneAccountTransaction())
41 ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
42 ->setMetadataValue(
43 'edge:type',
44 PhortuneAccountHasMemberEdgeType::EDGECONST)
45 ->setNewValue(
46 array(
47 '=' => array($actor->getPHID() => $actor->getPHID()),
48 ));
50 $editor = id(new PhortuneAccountEditor())
51 ->setActor($actor)
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);
59 unset($unguarded);
61 return $account;
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);
76 return $cart->save();
79 protected function getConfiguration() {
80 return array(
81 self::CONFIG_AUX_PHID => true,
82 self::CONFIG_COLUMN_SCHEMA => array(
83 'name' => 'text255',
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;
101 return $this;
104 public function getURI() {
105 return urisprintf(
106 '/phortune/account/%d/',
107 $this->getID());
110 public function getDetailsURI() {
111 return urisprintf(
112 '/phortune/account/%d/details/',
113 $this->getID());
116 public function getOrdersURI() {
117 return urisprintf(
118 '/phortune/account/%d/orders/',
119 $this->getID());
122 public function getOrderListURI($path = '') {
123 return urisprintf(
124 '/phortune/account/%d/orders/list/%s',
125 $this->getID(),
126 $path);
129 public function getSubscriptionsURI() {
130 return urisprintf(
131 '/phortune/account/%d/subscriptions/',
132 $this->getID());
135 public function getEmailAddressesURI() {
136 return urisprintf(
137 '/phortune/account/%d/addresses/',
138 $this->getID());
141 public function getPaymentMethodsURI() {
142 return urisprintf(
143 '/phortune/account/%d/methods/',
144 $this->getID());
147 public function getChargesURI() {
148 return urisprintf(
149 '/phortune/account/%d/charges/',
150 $this->getID());
153 public function getChargeListURI($path = '') {
154 return urisprintf(
155 '/phortune/account/%d/charges/list/%s',
156 $this->getID(),
157 $path);
160 public function attachMerchantPHIDs(array $merchant_phids) {
161 $this->merchantPHIDs = $merchant_phids;
162 return $this;
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)
176 ->save();
178 return $this;
181 public function isUserAccountMember(PhabricatorUser $user) {
182 $user_phid = $user->getPHID();
183 if (!$user_phid) {
184 return null;
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() {
208 return array(
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;
221 } else {
222 return PhabricatorPolicies::POLICY_NOONE;
227 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
228 if ($this->isUserAccountMember($viewer)) {
229 return true;
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(
239 $viewer_phids,
240 $merchant_phids);
242 if ($any_edit) {
243 return true;
247 return false;
250 public function describeAutomaticCapability($capability) {
251 return array(
252 pht('Members of an account can always view and edit it.'),
253 pht('Merchants an account has established a relationship can view it.'),