Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / people / storage / PhabricatorUserLog.php
blob61ccaff1edcb6f0bdff8afb8f3127e1df91e7e81
1 <?php
3 final class PhabricatorUserLog extends PhabricatorUserDAO
4 implements PhabricatorPolicyInterface {
6 protected $actorPHID;
7 protected $userPHID;
8 protected $action;
9 protected $oldValue;
10 protected $newValue;
11 protected $details = array();
12 protected $remoteAddr;
13 protected $session;
15 public static function initializeNewLog(
16 PhabricatorUser $actor = null,
17 $object_phid = null,
18 $action = null) {
20 $log = new PhabricatorUserLog();
22 if ($actor) {
23 $log->setActorPHID($actor->getPHID());
24 if ($actor->hasSession()) {
25 $session = $actor->getSession();
27 // NOTE: This is a hash of the real session value, so it's safe to
28 // store it directly in the logs.
29 $log->setSession($session->getSessionKey());
33 $log->setUserPHID((string)$object_phid);
34 $log->setAction($action);
36 $address = PhabricatorEnv::getRemoteAddress();
37 if ($address) {
38 $log->remoteAddr = $address->getAddress();
39 } else {
40 $log->remoteAddr = '';
43 return $log;
46 public static function loadRecentEventsFromThisIP($action, $timespan) {
47 $address = PhabricatorEnv::getRemoteAddress();
48 if (!$address) {
49 return array();
52 return id(new PhabricatorUserLog())->loadAllWhere(
53 'action = %s AND remoteAddr = %s AND dateCreated > %d
54 ORDER BY dateCreated DESC',
55 $action,
56 $address->getAddress(),
57 PhabricatorTime::getNow() - $timespan);
60 public function save() {
61 $this->details['host'] = php_uname('n');
62 $this->details['user_agent'] = AphrontRequest::getHTTPHeader('User-Agent');
64 return parent::save();
67 protected function getConfiguration() {
68 return array(
69 self::CONFIG_SERIALIZATION => array(
70 'oldValue' => self::SERIALIZATION_JSON,
71 'newValue' => self::SERIALIZATION_JSON,
72 'details' => self::SERIALIZATION_JSON,
74 self::CONFIG_COLUMN_SCHEMA => array(
75 'actorPHID' => 'phid?',
76 'action' => 'text64',
77 'remoteAddr' => 'text64',
78 'session' => 'text64?',
80 self::CONFIG_KEY_SCHEMA => array(
81 'actorPHID' => array(
82 'columns' => array('actorPHID', 'dateCreated'),
84 'userPHID' => array(
85 'columns' => array('userPHID', 'dateCreated'),
87 'action' => array(
88 'columns' => array('action', 'dateCreated'),
90 'dateCreated' => array(
91 'columns' => array('dateCreated'),
93 'remoteAddr' => array(
94 'columns' => array('remoteAddr', 'dateCreated'),
96 'session' => array(
97 'columns' => array('session', 'dateCreated'),
100 ) + parent::getConfiguration();
103 public function getURI() {
104 return urisprintf('/people/logs/%s/', $this->getID());
107 public function getObjectName() {
108 return pht('Activity Log %d', $this->getID());
111 public function getRemoteAddressForViewer(PhabricatorUser $viewer) {
112 $viewer_phid = $viewer->getPHID();
113 $actor_phid = $this->getActorPHID();
114 $user_phid = $this->getUserPHID();
116 if (!$viewer_phid) {
117 $can_see_ip = false;
118 } else if ($viewer->getIsAdmin()) {
119 $can_see_ip = true;
120 } else if ($viewer_phid == $actor_phid) {
121 // You can see the address if you took the action.
122 $can_see_ip = true;
123 } else if (!$actor_phid && ($viewer_phid == $user_phid)) {
124 // You can see the address if it wasn't authenticated and applied
125 // to you (partial login).
126 $can_see_ip = true;
127 } else {
128 // You can't see the address when an administrator disables your
129 // account, since it's their address.
130 $can_see_ip = false;
133 if (!$can_see_ip) {
134 return null;
137 return $this->getRemoteAddr();
141 /* -( PhabricatorPolicyInterface )----------------------------------------- */
144 public function getCapabilities() {
145 return array(
146 PhabricatorPolicyCapability::CAN_VIEW,
150 public function getPolicy($capability) {
151 switch ($capability) {
152 case PhabricatorPolicyCapability::CAN_VIEW:
153 return PhabricatorPolicies::POLICY_NOONE;
157 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
158 if ($viewer->getIsAdmin()) {
159 return true;
162 $viewer_phid = $viewer->getPHID();
163 if ($viewer_phid) {
164 $user_phid = $this->getUserPHID();
165 if ($viewer_phid == $user_phid) {
166 return true;
169 $actor_phid = $this->getActorPHID();
170 if ($viewer_phid == $actor_phid) {
171 return true;
175 return false;
178 public function describeAutomaticCapability($capability) {
179 return array(
180 pht('Users can view their activity and activity that affects them.'),
181 pht('Administrators can always view all activity.'),