module IdPlease module ModelExtensions module ForObject ## # Role check. # # @return [Boolean] Returns true if +subject+ has a role +role_name+ on this object. # # @param [Symbol,String] role_name Role name # @param [Subject] subject Subject to add role for # @see Acl9::ModelExtensions::Subject#has_role? def accepts_role?(role_name, subject, options = {}) subject.has_role?(role_name, self, options) end ## # Add role on the object to specified subject. # # @param [Symbol,String] role_name Role name # @param [Subject] subject Subject to add role for # @see Acl9::ModelExtensions::Subject#has_role! def accepts_role!(role_name, subject) subject.has_role!(role_name, self) end def list_subjects_all_roles(*args) options = args.extract_options! find_subjects(nil, options) end def list_subjects_for(role_name, *args) options = args.extract_options! find_subjects(role_name, options) end def method_missing(method_sym, *arguments, &block) # the first argument is a Symbol, so you need to_s it if you want to pattern match if method_sym.to_s =~ /^list_(.+)$/ list_subjects_for($1.singularize, *arguments) else super end end ## # Free specified subject of a role on this object. # # @param [Symbol,String] role_name Role name # @param [Subject] subject Subject to remove role from # @see Acl9::ModelExtensions::Subject#has_no_role! def accepts_no_role!(role_name, subject) subject.has_no_role!(role_name, self) end # ## # # Are there any roles for the specified +subject+ on this object? # # # # @param [Subject] subject Subject to query roles # # @return [Boolean] Returns true if +subject+ has any roles on this object. # # @see Acl9::ModelExtensions::Subject#has_roles_for? # def accepts_roles_by?(subject) # subject.has_roles_for? self # end # # alias :accepts_role_by? :accepts_roles_by? # # ## # # Which roles does +subject+ have on this object? # # # # @return [Array] Role instances, associated both with +subject+ and +object+ # # @param [Subject] subject Subject to query roles # # @see Acl9::ModelExtensions::Subject#roles_for # def accepted_roles_by(subject) # subject.roles_for self # end private def get_role(role_name, object) _auth_role_class.authorizable_eq(object).name_eq(role_name.to_s).first end def find_subjects(role_name = nil, options = {}) base_subjects = if role_name self._auth_assign_class.role_name_eq(role_name.to_s).role_authorizable_eq(self).all(:include => :subject).collect(&:subject).uniq else self._auth_assign_class.role_authorizable_eq(self).all(:include => :subject).collect(&:subject).uniq end if options[:check_groups] == false base_subjects else to_find = base_subjects.dup to_find.select { |subj| subj._auth_is_group }.each do |subj| base_subjects << subj.children end base_subjects.flatten.uniq end end protected def _auth_role_class self.class._auth_role_class_name.constantize end def _auth_assign_class self.class._auth_assign_class_name.constantize end end end end