Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
fixing a few issues in the previous commit which prevented user roles…
Browse files Browse the repository at this point in the history
… being deleted properly when either the role, or user was deleted.
  • Loading branch information
Kieran Pilkington committed Dec 24, 2008
1 parent 7b62677 commit a55d9c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion generators/role_model/templates/role_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# "moderator" for an instance of a model (i.e., an object), a model class,
# or without any specification at all.
class <%= class_name %> < ActiveRecord::Base
has_many :roles_users
has_many :roles_users, :dependent => :delete_all
has_many :users, :through => :roles_users
belongs_to :authorizable, :polymorphic => true
end
15 changes: 13 additions & 2 deletions lib/publishare/object_roles_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.included( recipient )

module ClassMethods
def acts_as_authorized_user(roles_relationship_opts = {})
has_many :roles_users
has_many :roles_users, :dependent => :delete_all
has_many :roles, :through => :roles_users
include Authorization::ObjectRolesTable::UserExtensions::InstanceMethods
include Authorization::Identity::UserExtensions::InstanceMethods # Provides all kinds of dynamic sugar via method_missing
Expand Down Expand Up @@ -125,10 +125,12 @@ def self.included( recipient )

module ClassMethods
def acts_as_authorizable
has_many :accepted_roles, :as => :authorizable, :class_name => 'Role', :dependent => :destroy
has_many :accepted_roles, :as => :authorizable, :class_name => 'Role'

has_many :users, :finder_sql => 'SELECT DISTINCT users.* FROM users INNER JOIN roles_users ON user_id = users.id INNER JOIN roles ON roles.id = role_id WHERE authorizable_type = \'#{self.class.base_class.to_s}\' AND authorizable_id = #{id}', :counter_sql => 'SELECT COUNT(DISTINCT users.id) FROM users INNER JOIN roles_users ON user_id = users.id INNER JOIN roles ON roles.id = role_id WHERE authorizable_type = \'#{self.class.base_class.to_s}\' AND authorizable_id = #{id}', :readonly => true

before_destroy :remove_user_roles

def accepts_role?( role_name, user )
user.has_role? role_name, self
end
Expand Down Expand Up @@ -182,6 +184,15 @@ def accepted_roles_by( user )
user.roles_for self
end

private

def remove_user_roles
self.accepted_roles.each do |role|
role.roles_users.delete_all
role.destroy
end
end

end
end

Expand Down

0 comments on commit a55d9c9

Please sign in to comment.