Skip to content

recording user last activity timestamp #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/travis/api/app/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def authorizer
end

def auth_for_repo(id, type)
current_user&.touch
permission = authorizer.for_repo(id, type)
halt 403, { error: { message: "We're sorry, but you're not authorized to perform this request" } } unless permission
rescue Travis::API::V3::AuthorizerError
Expand Down
1 change: 1 addition & 0 deletions lib/travis/api/v3/access_control/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class AccessControl::User < AccessControl::Generic
def initialize(user)
user = Models::User.find(user.id) if user.is_a? ::User
@user = user
user.touch
@access_permissions = user.permissions.where(user_id: user.id)
super()
end
Expand Down
4 changes: 4 additions & 0 deletions lib/travis/api/v3/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def installation
@installation = Models::Installation.find_by(owner_type: 'User', owner_id: id, removed_by_id: nil)
end

def touch
update(last_activity_at: Time.now) if last_activity_at.nil? || Time.now.utc - last_activity_at > 300
end

def internal?
!!get_internal_user
end
Expand Down
4 changes: 4 additions & 0 deletions lib/travis/model/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def with_email(email_address)
end
end

def touch
update(last_activity_at: Time.now) if last_activity_at.nil? || Time.now.utc - last_activity_at > 300
end

def token
tokens.first.try(:token)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/model/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def user(payload)
end
end

describe 'last activity' do
it 'contains last activity' do
user.save!
expect(user.last_activity_at).to be_nil
user.touch
expect(user.last_activity_at).to_not be_nil
end
end

describe 'avatar_url' do
it "returns avatar url if it's present" do
user.avatar_url = 'foo'
Expand Down
3 changes: 3 additions & 0 deletions spec/v3/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ module Routes
context 'when forcing authentication' do
before { Travis.config.force_authentication = true }
after { Travis.config.force_authentication = false }
before { User.last.update!(last_activity_at: nil) }

it 'does not allow access without authentication' do
get '/v3/examples'
expect(last_response.status).to eq 403
end

it 'does allow access with authentication' do
expect(User.last.last_activity_at).to be_nil
get '/v3/examples', {}, auth_headers
expect(last_response.status).to eq 200
expect(User.last.last_activity_at).to_not be_nil
end

it 'does allow access with log token' do
Expand Down