Added UserLog.

This commit is contained in:
Parziphal 2014-01-26 15:43:12 -05:00
parent 632a528baa
commit 41e5dcc8f4
5 changed files with 75 additions and 10 deletions

View File

@ -89,8 +89,9 @@ class UserController extends ApplicationController
}
if (current_user()->is_mod_or_higher()) {
# RP: Missing feature.
// $this->user_ips = $this->user->user_logs->order('created_at DESC').pluck('ip_addr').uniq
$this->user_ips = array();
$this->user_ips = array_unique(UserLog::where(['user_id' => $this->user->id])->order('created_at DESC')->take()->getAttributes('ip_addr'));
}
$tag_types = CONFIG()->tag_types;

View File

@ -46,15 +46,14 @@ class User extends Rails\ActiveRecord\Base
public function log($ip)
{
# iTODO: UserLog doesn't exist yet.
return;
return Rails::cache()->fetch(['type' => 'user_logs', 'id' => $this->id, 'ip' => $ip], ['expires_in' => '10 minutes'], function() use ($ip) {
Rails::cache()->fetch(['type' => 'user_logs', 'id' => 'all'], ['expires_in' => '1 day'], function() {
return UserLog::where('created_at < ?', date('Y-m-d 0:0:0', strtotime('-3 days')))->deleteAll();
# RP: Missing relation method "deleteAll()"
return UserLog::where('created_at < ?', date('Y-m-d 0:0:0', strtotime('-3 days')))->take()->each('destroy');
});
$log_entry = UserLog::where(['ip_addr' => $ip])->firstOrInitialize();
# RP: Missing feature.
$log_entry = UserLog::where(['ip_addr' => $ip, 'user_id' => $this->id])->firstOrInitialize();
$log_entry->created_at = date('Y-m-d H:i:s');
return $log_entry->save();
});
@ -65,10 +64,6 @@ class User extends Rails\ActiveRecord\Base
# characters in tags, they can be used to separate lines (with whitespace separating
# tags). Denormalizing this into a field in users would save a SQL query.
public $blacklisted_tags;
// protected function setBlacklistedTags($blacklists)
// {
// $this->('blacklisted_tags', $blacklists);
// }
public function blacklisted_tags()
{

12
app/models/UserLog.php Executable file
View File

@ -0,0 +1,12 @@
<?php
class UserLog extends Rails\ActiveRecord\Base
{
protected function associations()
{
return [
'belongs_to' => [
'user'
]
];
}
}

View File

@ -0,0 +1,23 @@
<?php
class CreateUserLog extends Rails\ActiveRecord\Migration\Base
{
public function up()
{
$this->execute(<<<EOS
CREATE TABLE `user_logs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`ip_addr` varchar(46) NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `created_at` (`created_at`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB
EOS
);
$this->execute(
"ALTER TABLE `user_logs`
ADD CONSTRAINT fk_user_logs__user_id FOREIGN KEY (user_id) REFERENCES `users`(id) ON DELETE CASCADE"
);
}
}

View File

@ -0,0 +1,34 @@
<?php
return array (
0 =>
array (
'id' =>
array (
'type' => 'int(11) unsigned',
'default' => NULL,
),
'user_id' =>
array (
'type' => 'int(11)',
'default' => NULL,
),
'ip_addr' =>
array (
'type' => 'varchar(46)',
'default' => NULL,
),
'created_at' =>
array (
'type' => 'datetime',
'default' => NULL,
),
),
1 =>
array (
'pri' =>
array (
0 => 'id',
),
),
)
;