Class: PostfixAdmin::Base
- Inherits:
-
Object
- Object
- PostfixAdmin::Base
- 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', }
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #add_account(address, password, in_name = nil) ⇒ Object
- #add_admin(username, password) ⇒ Object
- #add_admin_domain(user_name, domain_name) ⇒ Object
- #add_alias(address, goto) ⇒ Object
- #add_domain(domain_name) ⇒ Object
- #address_split(address) ⇒ Object
- #db_setup ⇒ Object
- #delete_account(address) ⇒ Object
- #delete_admin(user_name) ⇒ Object
- #delete_admin_domain(user_name, domain_name) ⇒ Object
- #delete_alias(address) ⇒ Object
- #delete_domain(domain_name) ⇒ Object
-
#initialize(config) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
18 19 20 21 22 23 24 25 |
# File 'lib/postfix_admin/base.rb', line 18 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' end |
Instance Attribute Details
#config ⇒ Object (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
90 91 92 93 94 95 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 |
# File 'lib/postfix_admin/base.rb', line 90 def add_account(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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/postfix_admin/base.rb', line 74 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/postfix_admin/base.rb', line 47 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/postfix_admin/base.rb', line 128 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/postfix_admin/base.rb', line 164 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
230 231 232 |
# File 'lib/postfix_admin/base.rb', line 230 def address_split(address) address.split('@') end |
#db_setup ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/postfix_admin/base.rb', line 27 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. end |
#delete_account(address) ⇒ Object
221 222 223 224 225 226 227 228 |
# File 'lib/postfix_admin/base.rb', line 221 def delete_account(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
211 212 213 214 215 216 217 218 219 |
# File 'lib/postfix_admin/base.rb', line 211 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
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/postfix_admin/base.rb', line 61 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
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/postfix_admin/base.rb', line 152 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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/postfix_admin/base.rb', line 183 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 |