]> git.openstreetmap.org Git - rails.git/blob - app/models/user_block.rb
Added a confirmation step to the process of granting and revoking user roles.
[rails.git] / app / models / user_block.rb
1 class UserBlock < ActiveRecord::Base
2   validate :moderator_permissions
3
4   belongs_to :user, :class_name => "User", :foreign_key => :user_id
5   belongs_to :moderator, :class_name => "User", :foreign_key => :moderator_id
6   belongs_to :revoker, :class_name => "User", :foreign_key => :revoker_id
7   
8   PERIODS = [0, 1, 3, 6, 12, 24, 48, 96]
9
10   ##
11   # returns true if the block is currently active (i.e: the user can't
12   # use the API).
13   def active?
14     needs_view or end_at > Time.now.getutc
15   end
16
17   ##
18   # revokes the block, allowing the user to use the API again. the argument
19   # is the user object who is revoking the ban.
20   def revoke!(revoker)
21     attrs = { :end_at => Time.now.getutc(),
22               :revoker_id => @user.id,
23               :needs_view => false }
24     revoker.moderator? and update_attributes(attrs)
25   end
26
27   private
28   ##
29   # validate that only moderators are allowed to change the
30   # block. this should be caught and dealt with in the controller,
31   # but i've also included it here just in case.
32   def moderator_permissions
33     errors.add_to_base("Must be a moderator to create or update a block.") if moderator_id_changed? and !moderator.moderator?
34     errors.add_to_base("Must be a moderator to revoke a block.") unless revoker_id.nil? or revoker.moderator?
35   end
36 end