Sha256: 93dfc9f3620587e31a16f2a5588e5bd73f27a0acee607c0aa4db2557c3ea4025

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'postfix_admin/concerns/dovecot_cram_md5_password'

module PostfixAdmin
  class Admin < ApplicationRecord
    self.table_name = :admin
    self.primary_key = :username

    include DovecotCramMD5Password

    validates :username, presence: true, uniqueness: { case_sensitive: false },
                         format: { with: RE_EMAIL_LIKE_WITH_ANCHORS,
                                   message: "must be a valid email address" }

    has_many :domain_admins, foreign_key: :username, dependent: :delete_all
    has_many :rel_domains, through: :domain_admins

    attr_accessor :domain_ids
    attribute :form_super_admin, :boolean, default: false

    # just in case
    validate on: :update do |admin|
      admin.errors.add(:username, 'cannot be changed') if admin.username_changed?
    end

    def super_admin?
      if @super_admin.nil?
        @super_admin = rel_domains.exists?("ALL")
      else
        @super_admin
      end
    end

    def super_admin=(value)
      if value
        domain_ids = self.rel_domain_ids.dup
        domain_ids << "ALL"
        self.rel_domain_ids = domain_ids
        save!
      else
        domain_admins.where(domain: "ALL").delete_all
      end
    end

    def has_admin?(admin)
      self == admin || super_admin?
    end

    def has_domain?(domain)
      !rel_domains.where(domain: ["ALL", domain.domain]).empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postfix_admin-0.2.0 lib/postfix_admin/admin.rb