Sha256: 357a1561b983c5cf84d68cc875678f54933c0e16045e3ce6326f7d3253d5bfc0

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

module UserConcerns
  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: { case_sensitive: false }
  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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wobauth-6.0.1 lib/wobauth/concerns/models/user_concerns.rb
wobauth-6.0.0 lib/wobauth/concerns/models/user_concerns.rb
wobauth-5.1.3 lib/wobauth/concerns/models/user_concerns.rb
wobauth-5.1.2 lib/wobauth/concerns/models/user_concerns.rb
wobauth-5.1.1 lib/wobauth/concerns/models/user_concerns.rb
wobauth-5.1.0 lib/wobauth/concerns/models/user_concerns.rb
wobauth-5.0.2 lib/wobauth/concerns/models/user_concerns.rb
wobauth-5.0.1 lib/wobauth/concerns/models/user_concerns.rb