Permalink
Browse files

Added user methods for API key management

  • Loading branch information...
skalnik committed Jun 30, 2010
1 parent 7964617 commit 7d09fc7289c86dd90714b8729b3edf7bcb3b2189
Showing with 30 additions and 0 deletions.
  1. +30 −0 app/models/user.rb
View
@@ -66,6 +66,28 @@ def hours(filter, date)
hours
end
+ def enable_api!
+ self.generate_api_key!
+ end
+
+ def disable_api!
+ self.update_attribute(:api_key, "")
+ end
+
+ def api_is_enabled?
+ !self.api_key.empty?
+ end
+
+ def self.authenticate(login, password)
+ return nil if login.blank? || password.blank?
+ if password.downcase == "x" # This is an API request
+ u = find_by_api_key(login)
+ else
+ u = find_by_login(login.downcase)
+ u && u.authenticated?(password) ? u : nil
+ end
+ end
+
protected
def with_memberships
project_ids = memberships.collect { |m| m.project_id }
@@ -84,4 +106,12 @@ def accessible_project_id?(project_id)
projects.collect { |p| p.id }.include?(project_id) :
projects.exists?(['projects.id = ?', project_id])
end
+
+ def secure_digest(*args)
+ Digest::SHA1.hexdigest(args.flatten.join('--'))
+ end
+
+ def generate_api_key!
+ self.updated_attribute(:api_key, secure_digest(Time.now, (1..10).map { rand.to_i }))
+ end
end

0 comments on commit 7d09fc7

Please sign in to comment.