Sha256: 95fa9f3eff53a2329960ec833f07437c5525abd019a2cf731262de07a03bb84d

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

module Wobauth
  module Concerns
    module Models::User
      extend ActiveSupport::Concern

      included do
	has_many :authorities, as: :authorizable, dependent: :destroy
	has_many :roles,       through: :authorities
	has_many :memberships, dependent: :destroy
	has_many :groups, -> { distinct }, through: :memberships
	has_many :group_roles, through: :groups, source: :roles
	has_many :group_authorities, through: :groups, source: :authorities

        validates :username, presence: true, uniqueness: true
      end


      def to_s
        if sn.blank? and givenname.blank?
          "#{username}" 
        elsif title.blank?
          "#{sn}, #{givenname} (#{username})"
        else
          "#{sn}, #{title} #{givenname} (#{username})"
        end
      end
  
      def name
        to_s
      end

      def is_admin?
        role?(:admin)
      end

      def role?(role)
        (self.authorities.joins(:role)
         .where("wobauth_roles.name = ?", role.to_s.camelize).present?) ||
        (self.group_authorities.joins(:role)
         .where("wobauth_roles.name = ?", role.to_s.camelize).present?)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wobauth-4.0.0 lib/concerns/models/user.rb
wobauth-3.4.8 lib/concerns/models/user.rb
wobauth-3.4.7 lib/concerns/models/user.rb
wobauth-3.4.6 lib/concerns/models/user.rb
wobauth-3.4.5 lib/concerns/models/user.rb
wobauth-3.4.4 lib/concerns/models/user.rb