postfix_admin 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,7 @@ module PostfixAdmin
16
16
  if stderr.readlines.to_s =~ /Fatal:/
17
17
  raise Error, stderr.readlines
18
18
  else
19
- stdout.readlines.first.chomp.gsub("{#{scheme}}", "")
19
+ stdout.readlines.first.chomp
20
20
  end
21
21
  end
22
22
 
@@ -0,0 +1,5 @@
1
+ module PostfixAdmin
2
+ class Log < ApplicationRecord
3
+ self.table_name = :log
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module PostfixAdmin
2
+ class MailDomain < ApplicationRecord
3
+ self.table_name = :domain
4
+ self.primary_key = :domain
5
+
6
+ has_many :addresses, class_name: "Mailbox", foreign_key: :domain,
7
+ dependent: :destroy
8
+ end
9
+ end
@@ -0,0 +1,89 @@
1
+ module PostfixAdmin
2
+ class Mailbox < ApplicationRecord
3
+ self.table_name = :mailbox
4
+ self.primary_key = :username
5
+
6
+ include DovecotCramMD5Password
7
+
8
+ attribute :quota_mb, :integer
9
+
10
+ validates :username, presence: true, uniqueness: { case_sensitive: false },
11
+ format: { with: RE_EMAIL_LIKE_WITH_ANCHORS,
12
+ message: "must be a valid email address" }
13
+ validates :maildir, presence: true, uniqueness: { case_sensitive: false }
14
+ validates :local_part, presence: true
15
+ validates :quota, presence: true,
16
+ numericality: { only_integer: true,
17
+ greater_than_or_equal_to: 0 }
18
+ validates :quota_mb, presence: true,
19
+ numericality: { only_integer: true,
20
+ greater_than_or_equal_to: 0 }
21
+
22
+ belongs_to :rel_domain, class_name: "Domain", foreign_key: :domain
23
+ has_one :alias, foreign_key: :address, dependent: :destroy
24
+ has_one :quota_usage, class_name: "Quota", foreign_key: :username,
25
+ dependent: :destroy
26
+
27
+ validate on: :create do |mailbox|
28
+ domain = mailbox.rel_domain
29
+ if !domain.mailboxes.zero? && domain.rel_mailboxes.count >= domain.mailboxes
30
+ message = "already has the maximum number of mailboxes " \
31
+ "(maximum is #{domain.mailboxes} mailboxes)"
32
+ mailbox.errors.add(:domain, message)
33
+ end
34
+ end
35
+
36
+ # just in case
37
+ validate on: :update do |mailbox|
38
+ mailbox.errors.add(:username, 'cannot be changed') if mailbox.username_changed?
39
+ mailbox.errors.add(:local_part, 'cannot be changed') if mailbox.local_part_changed?
40
+ end
41
+
42
+ validate do |mailbox|
43
+ domain = mailbox.rel_domain
44
+
45
+ unless domain.maxquota.zero?
46
+ if mailbox.quota_mb.zero?
47
+ mailbox.errors.add(:quota_mb, "cannot be 0")
48
+ elsif mailbox.quota_mb > domain.maxquota
49
+ message = "must be less than or equal to #{domain.maxquota} (MB)"
50
+ mailbox.errors.add(:quota_mb, message)
51
+ end
52
+ end
53
+ end
54
+
55
+ before_validation do |mailbox|
56
+ mailbox.name = "" if mailbox.name.nil?
57
+ if mailbox.quota_mb
58
+ mailbox.quota = mailbox.quota_mb * KB_TO_MB
59
+ elsif mailbox.quota
60
+ mailbox.quota_mb = mailbox.quota / KB_TO_MB
61
+ else
62
+ mailbox.quota_mb = 0
63
+ mailbox.quota = 0
64
+ end
65
+ mailbox.username = "#{mailbox.local_part}@#{mailbox.domain}"
66
+ mailbox.maildir = "#{mailbox.domain}/#{mailbox.username}/"
67
+ mailbox.build_alias(local_part: mailbox.local_part, goto: mailbox.username,
68
+ domain: mailbox.domain)
69
+ end
70
+
71
+ def quota_usage_str
72
+ if quota_usage
73
+ usage_mb = quota_usage.bytes / KB_TO_MB
74
+ usage_mb.to_s
75
+ else
76
+ "0"
77
+ end
78
+ end
79
+
80
+ def quota_str
81
+ if quota.zero?
82
+ "--"
83
+ else
84
+ quota_mb = quota / KB_TO_MB
85
+ "#{quota_mb} MB"
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,213 +1,10 @@
1
- require 'data_mapper'
2
-
3
- module PostfixAdmin
4
- def self.flag_str(flag)
5
- flag ? "YES" : "NO"
6
- end
7
-
8
- class Config
9
- include ::DataMapper::Resource
10
- property :id, Integer, :key => true
11
- property :name, String
12
- property :value, String
13
- storage_names[:default] = 'config'
14
- end
15
-
16
- class Admin
17
- include ::DataMapper::Resource
18
- property :username, String, :key => true, :length => 0..255
19
- property :password, String, :length => 0..255
20
- property :active, Boolean, :default => true
21
- property :created, DateTime, :default => DateTime.now
22
- property :modified, DateTime, :default => DateTime.now
23
-
24
- has n, :domain_admins, :child_key => :username
25
- has n, :domains, :model => 'Domain', :through => :domain_admins, :via => :domain
26
- storage_names[:default] = 'admin'
27
-
28
- def active_str
29
- PostfixAdmin.flag_str(active)
30
- end
31
-
32
- def has_domain?(domain_name)
33
- if super_admin?
34
- Domain.exist?(domain_name)
35
- else
36
- exist_domain?(domain_name)
37
- end
38
- end
39
-
40
- def super_admin=(value)
41
- if value
42
- domains << Domain.find('ALL')
43
- save or raise "Could not save ALL domain for Admin"
44
- else
45
- domain_admins(:domain_name => 'ALL').destroy or raise "Could not destroy DoaminAdmin for Admin"
46
- end
47
- end
48
-
49
- def super_admin?
50
- exist_domain?('ALL')
51
- end
52
-
53
- def clear_domains
54
- domains.clear
55
- save or raise "Could save Admin"
56
- end
57
-
58
- def self.find(username)
59
- Admin.first(:username => username)
60
- end
61
-
62
- def self.exist?(username)
63
- !!Admin.find(username)
64
- end
65
-
66
- private
67
-
68
- def exist_domain?(domain_name)
69
- !!domains.first(:domain_name => domain_name)
70
- end
71
- end
72
-
73
- class Domain
74
- include ::DataMapper::Resource
75
- property :domain_name, String, :field => 'domain', :key => true, :length => 0..255
76
- property :maxaliases, Integer, :field => 'aliases'
77
- property :maxmailboxes, Integer, :field => 'mailboxes'
78
- property :maxquota, Integer
79
- property :transport, String, :default => 'virtual', :length => 0..255
80
- property :backupmx, Integer, :default => 0
81
- property :description, String, :length => 0..255
82
- property :active, Boolean, :default => true
83
- property :created, DateTime, :default => DateTime.now
84
- property :modified, DateTime, :default => DateTime.now
85
-
86
- has n, :domain_admins, :child_key => :domain_name
87
- has n, :admins, :model => 'Admin', :through => :domain_admins
88
-
89
- has n, :mailboxes, :model => 'Mailbox', :child_key => :domain_name
90
- has n, :aliases, :model => 'Alias', :child_key => :domain_name
91
- storage_names[:default] = 'domain'
92
-
93
- def self.all_without_special_domain
94
- Domain.all(:domain_name.not => 'ALL')
95
- end
96
-
97
- def self.find(domain)
98
- Domain.first(:domain_name => domain)
99
- end
100
-
101
- def self.exist?(domain)
102
- !!Domain.find(domain)
103
- end
104
-
105
- def self.num_total_aliases
106
- Alias.count - Mailbox.count
107
- end
108
-
109
- def num_total_aliases
110
- aliases.count - mailboxes.count
111
- end
112
-
113
- def clear_admins
114
- admins.clear
115
- save or raise "Could not save Domain"
116
- end
117
-
118
- def active_str
119
- PostfixAdmin.flag_str(active)
120
- end
121
- end
122
-
123
- class DomainAdmin
124
- include ::DataMapper::Resource
125
- property :created, DateTime, :default => DateTime.now
126
- property :domain_name, String, :field => 'domain', :key => true, :length => 0..255
127
- property :username, String, :key => true, :length => 0..255
128
-
129
- belongs_to :domain, :model => 'Domain', :child_key => :domain_name
130
- belongs_to :admin, :model => 'Admin', :child_key => :username
131
- storage_names[:default] = 'domain_admins'
132
- end
133
-
134
- class Mailbox
135
- include ::DataMapper::Resource
136
- property :username, String, :key => true, :length => 0..255
137
- property :name, String, :length => 0..255
138
- property :domain_name, String, :field => 'domain'
139
- property :password, String, :length => 0..255
140
- property :maildir, String, :length => 0..255
141
- property :quota, Integer
142
- property :active, Boolean, :default => true
143
- property :created, DateTime, :default => DateTime.now
144
- property :modified, DateTime, :default => DateTime.now
145
-
146
- belongs_to :domain, :model => 'Domain', :child_key => :domain_name
147
-
148
- storage_names[:default] = 'mailbox'
149
- def active_str
150
- PostfixAdmin.flag_str(active)
151
- end
152
-
153
- def self.find(username)
154
- Mailbox.first(:username => username)
155
- end
156
-
157
- def self.exist?(username)
158
- !!Mailbox.find(username)
159
- end
160
- end
161
-
162
- class Alias
163
- include ::DataMapper::Resource
164
- property :address, String, :key => true, :length => 0..255
165
- property :goto, Text
166
- property :domain_name, String, :field => 'domain', :length => 0..255
167
- property :active, Boolean, :default => true
168
- property :created, DateTime, :default => DateTime.now
169
- property :modified, DateTime, :default => DateTime.now
170
-
171
- belongs_to :domain, :model => 'Domain', :child_key => :domain_name
172
-
173
- storage_names[:default] = 'alias'
174
-
175
- def active_str
176
- PostfixAdmin.flag_str(active)
177
- end
178
-
179
- def self.mailbox(address)
180
- mail_alias = Alias.new
181
- mail_alias.attributes = {
182
- :address => address,
183
- :goto => address,
184
- }
185
- mail_alias
186
- end
187
-
188
- def self.find(address)
189
- Alias.first(:address => address)
190
- end
191
-
192
- def self.exist?(address)
193
- !!Alias.find(address)
194
- end
195
-
196
- def mailbox?
197
- Mailbox.exist?(address)
198
- end
199
- end
200
-
201
- class Log
202
- include ::DataMapper::Resource
203
- property :timestamp, DateTime, key: true, default: DateTime.now
204
- property :username, String, length: 0..255
205
- property :domain_name, String, field: 'domain', length: 0..255
206
- property :action, String, length: 0..255
207
- property :data, Text
208
-
209
- belongs_to :domain, :model => 'Domain', :child_key => :domain_name
210
-
211
- storage_names[:default] = 'log'
212
- end
213
- end
1
+ require 'active_record'
2
+ require 'postfix_admin/application_record'
3
+ require 'postfix_admin/admin'
4
+ require 'postfix_admin/domain'
5
+ require 'postfix_admin/mailbox'
6
+ require 'postfix_admin/alias'
7
+ require 'postfix_admin/domain_admin'
8
+ require 'postfix_admin/log'
9
+ require 'postfix_admin/mail_domain'
10
+ require 'postfix_admin/quota'
@@ -0,0 +1,6 @@
1
+ module PostfixAdmin
2
+ class Quota < ApplicationRecord
3
+ self.table_name = :quota2
4
+ self.primary_key = :username
5
+ end
6
+ end
@@ -5,50 +5,54 @@ require 'postfix_admin/doveadm'
5
5
 
6
6
  module PostfixAdmin
7
7
  class Runner < Thor
8
+ def self.exit_on_failure?
9
+ true
10
+ end
11
+
8
12
  def initialize(*args)
9
13
  super
10
14
  @cli = CLI.new
11
15
  end
12
16
 
13
17
  desc "summary [example.com]", "Summarize the usage of PostfixAdmin"
14
- def summary(domain_name=nil)
15
- runner{ @cli.show_summary(domain_name) }
18
+ def summary(domain_name = nil)
19
+ runner { @cli.show_summary(domain_name) }
16
20
  end
17
21
 
18
22
  desc "schemes", "List all supported password schemes"
19
23
  def schemes
20
- runner{ puts PostfixAdmin::Doveadm.schemes.join(' ') }
24
+ runner { puts PostfixAdmin::Doveadm.schemes.join(' ') }
21
25
  end
22
26
 
23
27
  desc "show [example.com | admin@example.com | user@example.com]", "Show domains or admins or mailboxes"
24
- def show(name=nil)
25
- runner{ @cli.show(name) }
28
+ def show(name = nil)
29
+ runner { @cli.show(name) }
26
30
  end
27
31
 
28
32
  desc "setup example.com password", "Setup a domain"
29
33
  def setup(domain_name, password)
30
- runner{ @cli.setup_domain(domain_name, password) }
34
+ runner { @cli.setup_domain(domain_name, password) }
31
35
  end
32
36
 
33
37
  desc "admin_passwd admin@example.com new_password", "Change password of admin"
34
38
  def admin_passwd(user_name, password)
35
- runner{ @cli.change_admin_password(user_name, password) }
39
+ runner { @cli.change_admin_password(user_name, password) }
36
40
  end
37
41
 
38
42
  desc "account_passwd user@example.com new_password", "Change password of account"
39
43
  def account_passwd(user_name, password)
40
- runner{ @cli.change_account_password(user_name, password) }
44
+ runner { @cli.change_account_password(user_name, password) }
41
45
  end
42
46
 
43
47
  desc "add_domain example.com", "Add a domain"
44
48
  def add_domain(domain_name)
45
- runner{ @cli.add_domain(domain_name) }
49
+ runner { @cli.add_domain(domain_name) }
46
50
  end
47
51
 
48
52
  desc "edit_domain example.com", "Edit a domain limitation"
49
- method_option :aliases, :type => :numeric, :aliases => "-a", :desc => "Edit aliases limitation"
50
- method_option :mailboxes, :type => :numeric, :aliases => "-m", :desc => "Edit mailboxes limitation"
51
- method_option :maxquota, :type => :numeric, :aliases => "-q", :desc => "Edit max quota limitation"
53
+ method_option :aliases, type: :numeric, aliases: "-a", desc: "Edit aliases limitation"
54
+ method_option :mailboxes, type: :numeric, aliases: "-m", desc: "Edit mailboxes limitation"
55
+ method_option :maxquota, type: :numeric, aliases: "-q", desc: "Edit max quota limitation"
52
56
  method_option :active, type: :boolean, desc: "Update active status"
53
57
  def edit_domain(domain_name)
54
58
  runner do
@@ -63,22 +67,22 @@ module PostfixAdmin
63
67
 
64
68
  desc "delete_domain example.com", "Delete a domain"
65
69
  def delete_domain(domain_name)
66
- runner{ @cli.delete_domain(domain_name) }
70
+ runner { @cli.delete_domain(domain_name) }
67
71
  end
68
72
 
69
73
  desc "delete_admin admin@example.com", "Delete an admin"
70
74
  def delete_admin(user_name)
71
- runner{ @cli.delete_admin(user_name) }
75
+ runner { @cli.delete_admin(user_name) }
72
76
  end
73
77
 
74
78
  desc "delete_account user@example.com", "Delete an account"
75
79
  def delete_account(address)
76
- runner{ @cli.delete_account(address) }
80
+ runner { @cli.delete_account(address) }
77
81
  end
78
82
 
79
83
  desc "add_account user@example.com password", "Add an account"
80
- method_option :scheme, :type => :string, :aliases => "-s", :desc => "password scheme"
81
- method_option :name, :type => :string, :aliases => "-n", :desc => "full name"
84
+ method_option :scheme, type: :string, aliases: "-s", desc: "password scheme"
85
+ method_option :name, type: :string, aliases: "-n", desc: "full name"
82
86
  def add_account(address, password)
83
87
  runner do
84
88
  if options[:scheme] == 'scheme'
@@ -96,9 +100,9 @@ module PostfixAdmin
96
100
  end
97
101
 
98
102
  desc "edit_account user@example.com", "Edit an account"
99
- method_option :goto, :type => :string, :aliases => "-g", :desc => "mailboxes, addresses e-mails are delivered to"
100
- method_option :quota, :type => :numeric, :aliases => "-q", :desc => "quota limitation (MB)"
101
- method_option :name, :type => :string, :aliases => "-n", :desc => "full name"
103
+ method_option :goto, type: :string, aliases: "-g", desc: "mailboxes, addresses e-mails are delivered to"
104
+ method_option :quota, type: :numeric, aliases: "-q", desc: "quota limitation (MB)"
105
+ method_option :name, type: :string, aliases: "-n", desc: "full name"
102
106
  method_option :active, type: :boolean, desc: "Update active status"
103
107
  def edit_account(address)
104
108
  runner do
@@ -131,8 +135,8 @@ module PostfixAdmin
131
135
  end
132
136
 
133
137
  desc "add_admin admin@example.com password", "Add an admin user"
134
- method_option :super, :type => :boolean, :aliases => "-S", :desc => "register as a super admin"
135
- method_option :scheme, :type => :string, :aliases => "-s", :desc => "password scheme"
138
+ method_option :super, type: :boolean, aliases: "-S", desc: "register as a super admin"
139
+ method_option :scheme, type: :string, aliases: "-s", desc: "password scheme"
136
140
  def add_admin(user_name, password)
137
141
  runner do
138
142
  if options[:scheme] == 'scheme'
@@ -146,17 +150,17 @@ module PostfixAdmin
146
150
 
147
151
  desc "add_admin_domain admin@example.com example.com", "Add admin_domain"
148
152
  def add_admin_domain(user_name, domain_name)
149
- runner{ @cli.add_admin_domain(user_name, domain_name) }
153
+ runner { @cli.add_admin_domain(user_name, domain_name) }
150
154
  end
151
155
 
152
156
  desc "delete_admin_domain admin@example.com example.com", "Delete admin_domain"
153
157
  def delete_admin_domain(user_name, domain_name)
154
- runner{ @cli.delete_admin_domain(user_name, domain_name) }
158
+ runner { @cli.delete_admin_domain(user_name, domain_name) }
155
159
  end
156
160
 
157
161
  desc "edit_alias alias@example.com", "Edit an alias"
158
- method_option :goto, :type => :string, :aliases => "-g",
159
- :desc => "mailboxes, addresses e-mails are delivered to"
162
+ method_option :goto, type: :string, aliases: "-g",
163
+ desc: "mailboxes, addresses e-mails are delivered to"
160
164
  method_option :active, type: :boolean, desc: "Update active status"
161
165
  def edit_alias(address)
162
166
  runner do
@@ -171,38 +175,37 @@ module PostfixAdmin
171
175
 
172
176
  desc "add_alias alias@example.com goto@example.net", "Add an alias"
173
177
  def add_alias(address, goto)
174
- runner{ @cli.add_alias(address, goto) }
178
+ runner { @cli.add_alias(address, goto) }
175
179
  end
176
180
 
177
181
  desc "delete_alias alias@example.com", "Delete an alias"
178
182
  def delete_alias(address)
179
- runner{ @cli.delete_alias(address) }
183
+ runner { @cli.delete_alias(address) }
180
184
  end
181
185
 
182
186
  desc "log", "Show action logs"
183
187
  def log
184
- runner{ @cli.log }
188
+ runner { @cli.log }
185
189
  end
186
190
 
187
191
  desc "dump", "Dump all data"
188
192
  def dump
189
- runner{ @cli.dump }
193
+ runner { @cli.dump }
190
194
  end
191
195
 
192
196
  desc "version", "Show postfix_admin version"
193
197
  def version
194
198
  require 'postfix_admin/version'
195
- runner{ say "postfix_admin #{VERSION}" }
199
+ runner { say "postfix_admin #{VERSION}" }
196
200
  end
197
201
 
198
202
  private
199
203
 
200
204
  def runner
201
- begin
202
- yield
203
- rescue Error, ArgumentError => e
204
- abort e.message
205
- end
205
+ @cli.db_setup
206
+ yield
207
+ rescue StandardError => e
208
+ abort "Error: #{e.message}"
206
209
  end
207
210
  end
208
211
  end