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
}

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']   || 30
  @config[:mailboxes] = config['mailboxes'] || 30
  @config[:maxquota]  = config['maxquota']  || 100
  @config[:scheme]    = config['scheme']    || 'CRAM-MD5'
  @config[:passwordhash_prefix] = if config['passwordhash_prefix'].nil?
                                    true
                                  else
                                    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, in_name = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 96

def (address, password, in_name = nil)
  name = in_name || ''
  password_check(password)

  if address !~ /.+\@.+\..+/
    raise Error, "Invalid mail address #{address}"
  end
  user, domain_name = address_split(address)
  path = "#{domain_name}/#{address}/"

  unless Domain.exists?(domain_name)
    raise Error, "Could not find domain #{domain_name}"
  end

  if Alias.exists?(address)
    raise Error, "#{address} is already registered."
  end

  domain = Domain.find(domain_name)

  attributes = {
      username: address,
      password: password,
      name: name,
      maildir: path,
      local_part: user,
      quota_mb: @config[:maxquota]
  }

  mailbox = Mailbox.new(attributes)

  domain.rel_mailboxes << mailbox

  unless domain.save
    raise "Could not save Mailbox and Domain #{mailbox.errors.map(&:to_s).join} #{domain.errors.map(&:to_s).join}"
  end
end

#add_admin(username, password) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/postfix_admin/base.rb', line 80

def add_admin(username, password)
  password_check(password)

  if Admin.exists?(username)
    raise Error, "#{username} is already registered as admin."
  end
  admin = Admin.new
  admin.attributes = {
    username: username,
    password: password,
  }
  unless admin.save
    raise "Could not save Admin #{admin.errors.map(&:to_s).join}"
  end
end

#add_admin_domain(user_name, domain_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/postfix_admin/base.rb', line 53

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, "#{user_name} is already registered as admin of #{domain_name}."
  end

  admin.rel_domains << domain
  admin.save or raise "Relation Error: Domain of Admin"
end

#add_alias(address, goto) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/postfix_admin/base.rb', line 134

def add_alias(address, goto)
  if Mailbox.exists?(address)
    raise Error, "mailbox #{address} is already registered!"
  end
  if Alias.exists?(address)
    raise Error, "alias #{address} is already registered!"
  end

  local_part, domain_name = address_split(address)

  unless Domain.exists?(domain_name)
    raise Error, "Invalid domain! #{domain_name}"
  end

  domain = Domain.find(domain_name)

  attributes = {
    local_part: local_part,
    goto: goto
  }
  domain.rel_aliases << Alias.new(attributes)
  domain.save or raise "Could not save Alias"
end

#add_domain(domain_name) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/postfix_admin/base.rb', line 170

def add_domain(domain_name)
  domain_name = domain_name.downcase
  if domain_name !~ /.+\..+/
    raise Error, "Ivalid domain! #{domain_name}"
  end
  if Domain.exists?(domain_name)
    raise Error, "#{domain_name} is already registered!"
  end
  domain = Domain.new
  domain.attributes = {
    domain: domain_name,
    description: domain_name,
    aliases: @config[:aliases],
    mailboxes: @config[:mailboxes],
    maxquota: @config[:maxquota],
  }
  domain.save!
end

#address_split(address) ⇒ Object



236
237
238
# File 'lib/postfix_admin/base.rb', line 236

def address_split(address)
  address.split('@')
end

#db_setupObject



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

def db_setup
  raise "'database' parameter is required in '#{CLI.config_file}'" unless @config[:database]

  uri = URI.parse(@config[:database])

  if uri.scheme == "mysql"
    uri.scheme = "mysql2"
    warn("Deprecation Warning: Use 'mysql2' as a DB adopter instead of 'mysql' in '#{CLI.config_file}'")
  end

  if uri.scheme != "mysql2"
    raise "'#{uri.scheme}' is not supported as a DB adopter. Use 'mysql2' instead in '#{CLI.config_file}'."
  end

  ActiveRecord::Base.establish_connection(uri.to_s)

rescue LoadError => e
  raise e.message
end

#delete_account(address) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/postfix_admin/base.rb', line 227

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

  Mailbox.where(username: address).delete_all
  Alias.where(address: address).delete_all
end

#delete_admin(user_name) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/postfix_admin/base.rb', line 217

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.rel_domains.delete_all
  admin.destroy!
end

#delete_admin_domain(user_name, domain_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/postfix_admin/base.rb', line 67

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/postfix_admin/base.rb', line 158

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/postfix_admin/base.rb', line 189

def delete_domain(domain_name)
  domain_name = domain_name.downcase
  unless Domain.exists?(domain_name)
    raise Error, "Could not find domain #{domain_name}"
  end

  domain = Domain.find(domain_name)
  domain.rel_mailboxes.delete_all
  domain.rel_aliases.delete_all

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

  domain.admins.delete_all

  admin_names.each do |name|
    next unless Admin.exists?(name)

    admin = Admin.find(name)

    # check if the admin is needed or not
    if admin.rel_domains.empty?
      admin.destroy
    end
  end

  domain.destroy
end