Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / auth / storage / PhabricatorAuthInvite.php
blobf1b6d7ffecd787c8911002caa81207aface87708
1 <?php
3 final class PhabricatorAuthInvite
4 extends PhabricatorUserDAO
5 implements PhabricatorPolicyInterface {
7 protected $authorPHID;
8 protected $emailAddress;
9 protected $verificationHash;
10 protected $acceptedByPHID;
12 private $verificationCode;
13 private $viewerHasVerificationCode;
15 protected function getConfiguration() {
16 return array(
17 self::CONFIG_AUX_PHID => true,
18 self::CONFIG_COLUMN_SCHEMA => array(
19 'emailAddress' => 'sort128',
20 'verificationHash' => 'bytes12',
21 'acceptedByPHID' => 'phid?',
23 self::CONFIG_KEY_SCHEMA => array(
24 'key_address' => array(
25 'columns' => array('emailAddress'),
26 'unique' => true,
28 'key_code' => array(
29 'columns' => array('verificationHash'),
30 'unique' => true,
33 ) + parent::getConfiguration();
36 public function generatePHID() {
37 return PhabricatorPHID::generateNewPHID(
38 PhabricatorAuthInvitePHIDType::TYPECONST);
41 public function regenerateVerificationCode() {
42 $this->verificationCode = Filesystem::readRandomCharacters(16);
43 $this->verificationHash = null;
44 return $this;
47 public function getVerificationCode() {
48 if (!$this->verificationCode) {
49 if ($this->verificationHash) {
50 throw new Exception(
51 pht(
52 'Verification code can not be regenerated after an invite is '.
53 'created.'));
55 $this->regenerateVerificationCode();
57 return $this->verificationCode;
60 public function save() {
61 if (!$this->getVerificationHash()) {
62 $hash = PhabricatorHash::digestForIndex($this->getVerificationCode());
63 $this->setVerificationHash($hash);
66 return parent::save();
69 public function setViewerHasVerificationCode($loaded) {
70 $this->viewerHasVerificationCode = $loaded;
71 return $this;
75 /* -( PhabricatorPolicyInterface )----------------------------------------- */
78 public function getCapabilities() {
79 return array(
80 PhabricatorPolicyCapability::CAN_VIEW,
84 public function getPolicy($capability) {
85 switch ($capability) {
86 case PhabricatorPolicyCapability::CAN_VIEW:
87 return PhabricatorPolicies::POLICY_ADMIN;
91 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
92 if ($this->viewerHasVerificationCode) {
93 return true;
96 if ($viewer->getPHID()) {
97 if ($viewer->getPHID() == $this->getAuthorPHID()) {
98 // You can see invites you sent.
99 return true;
102 if ($viewer->getPHID() == $this->getAcceptedByPHID()) {
103 // You can see invites you have accepted.
104 return true;
108 return false;
111 public function describeAutomaticCapability($capability) {
112 return pht(
113 'Invites are visible to administrators, the inviting user, users with '.
114 'an invite code, and the user who accepts the invite.');