Class: PostfixAdmin::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/postfix_admin/base.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  "database" => "mysql2://postfix:password@localhost/postfix",
  "aliases" => 30,
  "mailboxes" => 30,
  "maxquota" => 100,
  "scheme" => "CRAM-MD5",
  "passwordhash_prefix" => true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/postfix_admin/base.rb', line 19

def initialize(config)
  @config = {}
  @config[:database]  = config["database"]
  @config[:aliases]   = config["aliases"]   || DEFAULT_CONFIG["aliases"]
  @config[:mailboxes] = config["mailboxes"] || DEFAULT_CONFIG["mailboxes"]
  @config[:maxquota]  = config["maxquota"]  || DEFAULT_CONFIG["maxquota"]
  @config[:scheme]    = config["scheme"]    || DEFAULT_CONFIG["scheme"]
  @config[:passwordhash_prefix] = if config.has_key?("passwordhash_prefix")
                                    config["passwordhash_prefix"]
                                  else
                                    DEFAULT_CONFIG["passwordhash_prefix"]
                                  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/postfix_admin/base.rb', line 8

def config
  @config
end

Instance Method Details

#add_account(address, password, name: "") ⇒ Object

Adds an email account that consists of a Mailbox and an Alias.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/postfix_admin/base.rb', line 92

def (address, password, name: "")
  (address, password)

  local_part, domain_name = address_split(address)
  domain_must_exist!(domain_name)

  attributes = {
    local_part: local_part,
    domain: domain_name,
    password: password,
    name: name,
    quota: @config[:maxquota] * KB_TO_MB
  }

  # An Alias also will be added when a Mailbox is saved.
  mailbox = Mailbox.new(attributes)

  raise_save_error(mailbox) unless mailbox.save
end

#add_admin(username, password) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/postfix_admin/base.rb', line 75

def add_admin(username, password)
  validate_password(password)

  if Admin.exists?(username)
    raise_error "Admin has already been registered: #{username}"
  end

  admin = Admin.new
  admin.attributes = {
    username: username,
    password: password
  }

  raise_save_error(admin) unless admin.save
end

#add_admin_domain(user_name, domain_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/postfix_admin/base.rb', line 48

def add_admin_domain(user_name, domain_name)
  admin_domain_check(user_name, domain_name)

  admin  = Admin.find(user_name)
  domain = Domain.find(domain_name)

  if admin.has_domain?(domain)
    raise_error "Admin '#{user_name}' has already been registered for Domain '#{domain_name}'"
  end

  admin.rel_domains << domain
  admin.save || raise_error("Relation Error: Domain of Admin")
end

#add_alias(address, goto) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/postfix_admin/base.rb', line 112

def add_alias(address, goto)
  if Mailbox.exists?(address)
    raise_error "Mailbox has already been registered: #{address}"
  end

  alias_must_not_exist!(address)

  local_part, domain_name = address_split(address)

  domain = find_domain(domain_name)

  attributes = {
    address: address,
    goto: goto,
    domain: domain_name
  }

  new_alias = Alias.new(attributes)

  raise_save_error(new_alias) unless new_alias.save
end

#add_domain(domain_name, description: nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/postfix_admin/base.rb', line 146

def add_domain(domain_name, description: nil)
  domain_name = domain_name.downcase

  unless valid_domain_name?(domain_name)
    raise_error "Invalid domain name: #{domain_name}"
  end

  if Domain.exists?(domain_name)
    raise_error "Domain has already been registered: #{domain_name}"
  end

  new_description = description || ""

  domain = Domain.new
  domain.attributes = {
    domain: domain_name,
    description: new_description,
    aliases: @config[:aliases],
    mailboxes: @config[:mailboxes],
    maxquota: @config[:maxquota]
  }
  domain.save!
end

#db_setupObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/postfix_admin/base.rb', line 33

def db_setup
  database = ENV.fetch("DATABASE_URL") { @config[:database] }

  unless database
    raise_error "'database' parameter is required in '#{CLI.config_file}' or specify 'DATABASE_URL' environment variable"
  end

  uri = URI.parse(database)

  ActiveRecord::Base.establish_connection(uri.to_s)

rescue LoadError => e
  raise_error e.message
end

#delete_account(address) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/postfix_admin/base.rb', line 189

def (address)
  unless Alias.exists?(address) && Mailbox.exists?(address)
    raise_error "Could not find account: #{address}"
  end

  mailbox = Mailbox.find(address)
  mailbox.destroy!
end

#delete_admin(user_name) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/postfix_admin/base.rb', line 180

def delete_admin(user_name)
  unless Admin.exists?(user_name)
    raise_error "Could not find admin #{user_name}"
  end

  admin = Admin.find(user_name)
  admin.destroy!
end

#delete_admin_domain(user_name, domain_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/postfix_admin/base.rb', line 62

def delete_admin_domain(user_name, domain_name)
  admin_domain_check(user_name, domain_name)

  admin = Admin.find(user_name)
  domain_admin_query = admin.domain_admins.where(domain: domain_name)

  unless domain_admin_query.take
    raise_error "#{user_name} is not registered as admin of #{domain_name}."
  end

  domain_admin_query.delete_all
end

#delete_alias(address) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/postfix_admin/base.rb', line 134

def delete_alias(address)
  if Mailbox.exists?(address)
    raise_error "Can not delete mailbox by delete_alias. Use delete_account"
  end

  unless Alias.exists?(address)
    raise_error "#{address} is not found!"
  end

  Alias.where(address: address).delete_all
end

#delete_domain(domain_name) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/postfix_admin/base.rb', line 170

def delete_domain(domain_name)
  domain_name = domain_name.downcase

  domain = find_domain(domain_name)

  admin_names = domain.admins.map(&:username)

  domain.destroy!
end