diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6c3d4924c4..b5e8efcf81 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -752,7 +752,7 @@ App::get('/v1/account/logs') $clientEngine = (isset($client['engine'])) ? $client['engine'] : ''; $clientEngineVersion = (isset($client['engine_version'])) ? $client['engine_version'] : ''; - $output[$i] = [ + $output[$i] = new Document([ 'event' => $log['event'], 'ip' => $log['ip'], 'time' => \strtotime($log['time']), @@ -769,15 +769,15 @@ App::get('/v1/account/logs') 'deviceName' => $dd->getDeviceName(), 'deviceBrand' => $dd->getBrandName(), 'deviceModel' => $dd->getModel(), - ]; + ]); try { $record = $geodb->country($log['ip']); - $output[$i]['countryCode'] = \strtolower($record->country->isoCode); - $output[$i]['countryName'] = (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : $locale->getText('locale.country.unknown'); + $output[$i]->setAttribute('countryCode', \strtolower($record->country->isoCode)); + $output[$i]->setAttribute('countryName', (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : $locale->getText('locale.country.unknown')); } catch (\Exception $e) { - $output[$i]['countryCode'] = '--'; - $output[$i]['countryName'] = $locale->getText('locale.country.unknown'); + $output[$i]->setAttribute('countryCode', '--'); + $output[$i]->setAttribute('countryName', $locale->getText('locale.country.unknown')); } } diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 19f1375f57..ee4061bd55 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -264,8 +264,7 @@ App::get('/v1/users/:userId/logs') $clientEngine = (isset($client['engine'])) ? $client['engine'] : ''; $clientEngineVersion = (isset($client['engine_version'])) ? $client['engine_version'] : ''; - - $output[$i] = [ + $output[$i] = new Document([ 'event' => $log['event'], 'ip' => $log['ip'], 'time' => \strtotime($log['time']), @@ -282,15 +281,15 @@ App::get('/v1/users/:userId/logs') 'deviceName' => $dd->getDeviceName(), 'deviceBrand' => $dd->getBrandName(), 'deviceModel' => $dd->getModel(), - ]; + ]); try { $record = $geodb->country($log['ip']); - $output[$i]['countryCode'] = \strtolower($record->country->isoCode); - $output[$i]['countryName'] = (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : $locale->getText('locale.country.unknown'); + $output[$i]->setAttribute('countryCode', \strtolower($record->country->isoCode)); + $output[$i]->setAttribute('countryName', (isset($countries[$record->country->isoCode])) ? $countries[$record->country->isoCode] : $locale->getText('locale.country.unknown')); } catch (\Exception $e) { - $output[$i]['countryCode'] = '--'; - $output[$i]['countryName'] = $locale->getText('locale.country.unknown'); + $output[$i]->setAttribute('countryCode', '--'); + $output[$i]->setAttribute('countryName', $locale->getText('locale.country.unknown')); } } diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 5cc4af1963..a668f8c80e 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -19,6 +19,7 @@ use Appwrite\Utopia\Response\Model\User; use Appwrite\Utopia\Response\Model\Session; use Appwrite\Utopia\Response\Model\Team; use Appwrite\Utopia\Response\Model\Locale; +use Appwrite\Utopia\Response\Model\Log; use Appwrite\Utopia\Response\Model\Membership; use Appwrite\Utopia\Response\Model\Platform; use Appwrite\Utopia\Response\Model\Tag; @@ -112,6 +113,7 @@ class Response extends SwooleResponse ->setModel(new BaseList('Platforms List', self::MODEL_PLATFORM_LIST, 'platforms', self::MODEL_PLATFORM)) ->setModel(new BaseList('Domains List', self::MODEL_DOMAIN_LIST, 'domains', self::MODEL_DOMAIN)) // Entities + ->setModel(new Log()) ->setModel(new User()) ->setModel(new Session()) ->setModel(new Locale()) @@ -126,11 +128,9 @@ class Response extends SwooleResponse ->setModel(new Task()) ->setModel(new Domain()) ->setModel(new Platform()) - // Locale // Continent // Country // Currency - // Log // Verification // Recovery // Language @@ -209,7 +209,11 @@ class Response extends SwooleResponse } foreach ($data[$key] as &$item) { - if(array_key_exists($rule['type'], $this->models) && $item instanceof Document) { + if(!array_key_exists($rule['type'], $this->models)) { + throw new Exception('Missing model for rule: '. $rule['type']); + } + + if($item instanceof Document) { $item = $this->output($item, $rule['type']); } } diff --git a/src/Appwrite/Utopia/Response/Model/Log.php b/src/Appwrite/Utopia/Response/Model/Log.php index d1a8b3bba0..35040d018e 100644 --- a/src/Appwrite/Utopia/Response/Model/Log.php +++ b/src/Appwrite/Utopia/Response/Model/Log.php @@ -9,6 +9,107 @@ class Log extends Model { public function __construct() { + $this + ->addRule('event', [ + 'type' => 'string', + 'description' => 'Event name.', + 'example' => 'account.sessions.create', + ]) + ->addRule('ip', [ + 'type' => 'string', + 'description' => 'IP session in use when the session was created.', + 'example' => '127.0.0.1', + ]) + ->addRule('time', [ + 'type' => 'integer', + 'description' => 'Log creation time in Unix timestamp.', + 'example' => 1592981250, + ]) + ->addRule('osCode', [ + 'type' => 'string', + 'description' => 'Operating system code name. View list of [available options](https://github.com/appwrite/appwrite/blob/master/docs/lists/os.json).', + 'default' => '', + 'example' => 'Mac', + ]) + ->addRule('osName', [ + 'type' => 'string', + 'description' => 'Operating system name.', + 'default' => '', + 'example' => 'Mac', + ]) + ->addRule('osVersion', [ + 'type' => 'string', + 'description' => 'Operating system version.', + 'default' => '', + 'example' => 'Mac', + ]) + ->addRule('clientType', [ + 'type' => 'string', + 'description' => 'Client type.', + 'default' => '', + 'example' => 'browser', + ]) + ->addRule('clientCode', [ + 'type' => 'string', + 'description' => 'Client code name. View list of [available options](https://github.com/appwrite/appwrite/blob/master/docs/lists/clients.json).', + 'default' => '', + 'example' => 'CM', + ]) + ->addRule('clientName', [ + 'type' => 'string', + 'description' => 'Client name.', + 'default' => '', + 'example' => 'Chrome Mobile iOS', + ]) + ->addRule('clientVersion', [ + 'type' => 'string', + 'description' => 'Client version.', + 'default' => '', + 'example' => '84.0', + ]) + ->addRule('clientEngine', [ + 'type' => 'string', + 'description' => 'Client engine name.', + 'default' => '', + 'example' => 'WebKit', + ]) + ->addRule('clientEngineVersion', [ + 'type' => 'string', + 'description' => 'Client engine name.', + 'default' => '', + 'example' => '605.1.15', + ]) + ->addRule('deviceName', [ + 'type' => 'string', + 'description' => 'Device name.', + 'default' => '', + 'example' => 'smartphone', + ]) + ->addRule('deviceBrand', [ + 'type' => 'string', + 'description' => 'Device brand name.', + 'default' => '', + 'example' => 'Google', + ]) + ->addRule('deviceModel', [ + 'type' => 'string', + 'description' => 'Device model name.', + 'default' => '', + 'example' => 'Nexus 5', + ]) + ->addRule('countryCode', [ + 'type' => 'string', + 'description' => 'Country two-character ISO 3166-1 alpha code.', + 'default' => '', + 'example' => 'US', + ]) + ->addRule('countryName', [ + 'type' => 'string', + 'description' => 'Country name.', + 'default' => '', + 'example' => 'United States', + ]) + ; } /** diff --git a/src/Appwrite/Utopia/Response/Model/Session.php b/src/Appwrite/Utopia/Response/Model/Session.php index 39fa8c1380..2e22513fdf 100644 --- a/src/Appwrite/Utopia/Response/Model/Session.php +++ b/src/Appwrite/Utopia/Response/Model/Session.php @@ -23,7 +23,7 @@ class Session extends Model ]) ->addRule('ip', [ 'type' => 'string', - 'description' => 'IP session in use when the session was created.', + 'description' => 'IP in use when the session was created.', 'default' => '', 'example' => '127.0.0.1', ])