Path: blob/master/src/applications/people/storage/PhabricatorUserLog.php
12256 views
<?php12final class PhabricatorUserLog extends PhabricatorUserDAO3implements PhabricatorPolicyInterface {45protected $actorPHID;6protected $userPHID;7protected $action;8protected $oldValue;9protected $newValue;10protected $details = array();11protected $remoteAddr;12protected $session;1314public static function initializeNewLog(15PhabricatorUser $actor = null,16$object_phid = null,17$action = null) {1819$log = new PhabricatorUserLog();2021if ($actor) {22$log->setActorPHID($actor->getPHID());23if ($actor->hasSession()) {24$session = $actor->getSession();2526// NOTE: This is a hash of the real session value, so it's safe to27// store it directly in the logs.28$log->setSession($session->getSessionKey());29}30}3132$log->setUserPHID((string)$object_phid);33$log->setAction($action);3435$address = PhabricatorEnv::getRemoteAddress();36if ($address) {37$log->remoteAddr = $address->getAddress();38} else {39$log->remoteAddr = '';40}4142return $log;43}4445public static function loadRecentEventsFromThisIP($action, $timespan) {46$address = PhabricatorEnv::getRemoteAddress();47if (!$address) {48return array();49}5051return id(new PhabricatorUserLog())->loadAllWhere(52'action = %s AND remoteAddr = %s AND dateCreated > %d53ORDER BY dateCreated DESC',54$action,55$address->getAddress(),56PhabricatorTime::getNow() - $timespan);57}5859public function save() {60$this->details['host'] = php_uname('n');61$this->details['user_agent'] = AphrontRequest::getHTTPHeader('User-Agent');6263return parent::save();64}6566protected function getConfiguration() {67return array(68self::CONFIG_SERIALIZATION => array(69'oldValue' => self::SERIALIZATION_JSON,70'newValue' => self::SERIALIZATION_JSON,71'details' => self::SERIALIZATION_JSON,72),73self::CONFIG_COLUMN_SCHEMA => array(74'actorPHID' => 'phid?',75'action' => 'text64',76'remoteAddr' => 'text64',77'session' => 'text64?',78),79self::CONFIG_KEY_SCHEMA => array(80'actorPHID' => array(81'columns' => array('actorPHID', 'dateCreated'),82),83'userPHID' => array(84'columns' => array('userPHID', 'dateCreated'),85),86'action' => array(87'columns' => array('action', 'dateCreated'),88),89'dateCreated' => array(90'columns' => array('dateCreated'),91),92'remoteAddr' => array(93'columns' => array('remoteAddr', 'dateCreated'),94),95'session' => array(96'columns' => array('session', 'dateCreated'),97),98),99) + parent::getConfiguration();100}101102public function getURI() {103return urisprintf('/people/logs/%s/', $this->getID());104}105106public function getObjectName() {107return pht('Activity Log %d', $this->getID());108}109110public function getRemoteAddressForViewer(PhabricatorUser $viewer) {111$viewer_phid = $viewer->getPHID();112$actor_phid = $this->getActorPHID();113$user_phid = $this->getUserPHID();114115if (!$viewer_phid) {116$can_see_ip = false;117} else if ($viewer->getIsAdmin()) {118$can_see_ip = true;119} else if ($viewer_phid == $actor_phid) {120// You can see the address if you took the action.121$can_see_ip = true;122} else if (!$actor_phid && ($viewer_phid == $user_phid)) {123// You can see the address if it wasn't authenticated and applied124// to you (partial login).125$can_see_ip = true;126} else {127// You can't see the address when an administrator disables your128// account, since it's their address.129$can_see_ip = false;130}131132if (!$can_see_ip) {133return null;134}135136return $this->getRemoteAddr();137}138139140/* -( PhabricatorPolicyInterface )----------------------------------------- */141142143public function getCapabilities() {144return array(145PhabricatorPolicyCapability::CAN_VIEW,146);147}148149public function getPolicy($capability) {150switch ($capability) {151case PhabricatorPolicyCapability::CAN_VIEW:152return PhabricatorPolicies::POLICY_NOONE;153}154}155156public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {157if ($viewer->getIsAdmin()) {158return true;159}160161$viewer_phid = $viewer->getPHID();162if ($viewer_phid) {163$user_phid = $this->getUserPHID();164if ($viewer_phid == $user_phid) {165return true;166}167168$actor_phid = $this->getActorPHID();169if ($viewer_phid == $actor_phid) {170return true;171}172}173174return false;175}176177public function describeAutomaticCapability($capability) {178return array(179pht('Users can view their activity and activity that affects them.'),180pht('Administrators can always view all activity.'),181);182}183184}185186187