postfix_admin 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,24 +1,34 @@
1
1
  # PostfixAdmin
2
2
 
3
- TODO: Write a gem description
3
+ Command Line Tools of Postfix Admin
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'postfix_admin'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
7
+ Install postfix_admin as:
16
8
 
17
9
  $ gem install postfix_admin
18
10
 
19
11
  ## Usage
20
12
 
21
- TODO: Write usage instructions here
13
+ ```
14
+ postfix_admin account_passwd user@example.com new_password # Change password of account
15
+ postfix_admin add_account user@example.com password # Add an account
16
+ postfix_admin add_admin admin@example.com password # Add an admin user
17
+ postfix_admin add_admin_domain admin@example.com example.com # Add admin_domain
18
+ postfix_admin add_alias alias@example.com goto@example.net # Add an alias
19
+ postfix_admin add_domain example.com # Add a domain
20
+ postfix_admin admin_passwd admin@example.com new_password # Change password of admin
21
+ postfix_admin delete_account user@example.com # Delete an account
22
+ postfix_admin delete_admin admin@example.com # Delete an admin
23
+ postfix_admin delete_alias alias@example.com # Delete an alias
24
+ postfix_admin delete_domain example.com # Delete a domain
25
+ postfix_admin help [TASK] # Describe available tasks or one specific task
26
+ postfix_admin setup example.com password # Setup a domain
27
+ postfix_admin show # List of domains
28
+ postfix_admin summary [example.com] # Summarize the usage of PostfixAdmin
29
+ postfix_admin super_admin admin@example.com # Enable super admin flag of an admin
30
+ postfix_admin version # Show postfix_admin version
31
+ ```
22
32
 
23
33
  ## Contributing
24
34
 
data/Thorfile ADDED
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'bundler'
5
+ require 'thor/rake_compat'
6
+
7
+ class Default < Thor
8
+ include Thor::RakeCompat
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ desc "build", "Build postfix_admin-#{PostfixAdmin::VERSION}.gem into the pkg directory"
12
+ def build
13
+ Rake::Task["build"].execute
14
+ end
15
+
16
+ desc "install", "Build and install postfix_admin-#{PostfixAdmin::VERSION}.gem into system gems"
17
+ def install
18
+ Rake::Task["install"].execute
19
+ end
20
+
21
+ desc "release", "Create tag v#{PostfixAdmin::VERSION} and build and push postfix_admin-#{PostfixAdmin::VERSION}.gem to Rubygems"
22
+ def release
23
+ Rake::Task["release"].execute
24
+ end
25
+
26
+ desc "spec", "Run RSpec code examples"
27
+ def spec
28
+ exec "rspec --color --format=documentation spec"
29
+ end
30
+ end
data/bin/postfix_admin CHANGED
@@ -1,101 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'thor'
4
- require 'postfix_admin'
5
- require 'postfix_admin/cli'
3
+ require 'postfix_admin/runner'
6
4
 
7
- class PostfixAdminCommand < Thor
8
- def initialize(*args)
9
- super
10
- @cli = PostfixAdmin::CLI.new
11
- end
12
- desc "show", "List of domains"
13
- def show(domain=nil)
14
- if domain
15
- @cli.show_domain_account(domain)
16
- else
17
- @cli.show_domain
18
- @cli.show_admin
19
- end
20
- end
21
-
22
- desc "add_domain", "add a domain"
23
- def add_domain(domain=nil)
24
- if domain
25
- if @cli.add_domain(domain)
26
- puts %Q!"#{domain}" is successfully registered.!
27
- end
28
- @cli.show_domain
29
- else
30
- puts "USAGE: #{$0} add_domain example.com"
31
- exit
32
- end
33
- end
34
-
35
- desc "delete_domain", "delete a domain"
36
- def delete_domain(domain=nil)
37
- if domain
38
- if @cli.delete_domain(domain)
39
- puts %Q!"#{domain}" is successfully deleted.!
40
- end
41
- @cli.show_domain
42
- else
43
- puts "USAGE: #{$0} delete_domain example.com"
44
- exit
45
- end
46
- end
47
-
48
- desc "add_account", "add an account"
49
- def add_account(address=nil,password=nil)
50
- if address && password
51
- if @cli.add_account(address, password)
52
- puts %Q!"#{address}" is successfully registered.!
53
- end
54
- @cli.show_domain_account(address.split(/@/)[1])
55
- else
56
- puts "USAGE: #{$0} add_account user@example.com password"
57
- exit
58
- end
59
- end
60
-
61
- desc "add_admin", "add an admin user"
62
- def add_admin(user_name=nil, password=nil)
63
- if user_name && password
64
- if @cli.add_admin(user_name, password)
65
- puts %Q!"#{user_name}" is successfully registered as admin.!
66
- end
67
- @cli.show_admin
68
- else
69
- puts "USAGE: #{$0} add_admin user@example.com password"
70
- exit
71
- end
72
- end
73
-
74
- desc "add_admin_domain", "add admin_domain"
75
- def add_admin_domain(user_name=nil, domain=nil)
76
- if user_name && domain
77
- if @cli.add_admin_domain(user_name, domain)
78
- puts %Q!"#{domain}" is appended in the domains of #{user_name}.!
79
- end
80
- @cli.show_admin_domain(user_name)
81
- else
82
- puts "USAGE: #{$0} add_admin_domain user@example.com example.com"
83
- exit
84
- end
85
- end
86
-
87
- desc "add_alias", "add an alias"
88
- def add_alias(address=nil, goto=nil)
89
- if address && goto
90
- if @cli.add_alias(address, goto)
91
- puts %Q!"#{address}: #{goto}" is successfully registered as alias.!
92
- end
93
- @cli.show_domain_account(address.split(/@/)[1])
94
- else
95
- puts "USAGE: #{$0} add_alias user@example.com goto@example.com"
96
- exit
97
- end
98
- end
5
+ begin
6
+ PostfixAdmin::Runner.start
7
+ rescue => e
8
+ warn e.message
99
9
  end
100
-
101
- PostfixAdminCommand.start
@@ -0,0 +1,188 @@
1
+ require 'postfix_admin/models'
2
+ require 'postfix_admin/error'
3
+
4
+ require 'date'
5
+ require 'data_mapper'
6
+
7
+ module PostfixAdmin
8
+ class Base
9
+ attr_reader :config
10
+
11
+ DEFAULT_CONFIG = {
12
+ 'database' => 'mysql://postfix:password@localhost/postfix',
13
+ 'aliases' => 30,
14
+ 'mailboxes' => 30,
15
+ 'maxquota' => 100
16
+ }
17
+
18
+ def initialize(config)
19
+ db_setup(config['database'])
20
+ @config = {}
21
+ @config[:aliases] = config['aliases'] || 30
22
+ @config[:mailboxes] = config['mailboxes'] || 30
23
+ @config[:maxquota] = config['maxquota'] || 100
24
+ @config[:mailbox_quota] = @config[:maxquota] * 1024 * 1000
25
+ end
26
+
27
+ def db_setup(database)
28
+ DataMapper.setup(:default, database)
29
+ DataMapper.finalize
30
+ end
31
+
32
+ def add_admin_domain(username, domain_name)
33
+ unless Admin.exist?(username)
34
+ raise Error, "#{username} is not resistered as admin."
35
+ end
36
+ unless Domain.exist?(domain_name)
37
+ raise Error, "Could not find domain #{domain_name}"
38
+ end
39
+
40
+ admin = Admin.find(username)
41
+ if admin.has_domain?(domain_name)
42
+ raise Error, "#{username} is already resistered as admin of #{domain_name}."
43
+ end
44
+
45
+ domain = Domain.find(domain_name)
46
+ admin.domains << domain
47
+ admin.save or raise "Relation Error: Domain of Admin"
48
+ end
49
+
50
+ def add_admin(username, password)
51
+ if Admin.exist?(username)
52
+ raise Error, "#{username} is already resistered as admin."
53
+ end
54
+ admin = Admin.new
55
+ admin.attributes = {
56
+ :username => username,
57
+ :password => password,
58
+ }
59
+ admin.save or raise "Could not save Admin"
60
+ end
61
+
62
+ def add_account(address, password)
63
+ if address !~ /.+\@.+\..+/
64
+ raise Error, "Invalid mail address #{address}"
65
+ end
66
+ user, domain_name = address_split(address)
67
+ path = "#{domain_name}/#{address}/"
68
+
69
+ unless Domain.exist?(domain_name)
70
+ raise Error, "Could not find domain #{domain_name}"
71
+ end
72
+
73
+ if Alias.exist?(address)
74
+ raise Error, "#{address} is already resistered."
75
+ end
76
+
77
+ domain = Domain.find(domain_name)
78
+ mail_alias = Alias.new
79
+ mail_alias.attributes = {
80
+ :address => address,
81
+ :goto => address,
82
+ }
83
+ domain.aliases << mail_alias
84
+
85
+ mailbox = Mailbox.new
86
+ mailbox.attributes = {
87
+ :username => address,
88
+ :password => password,
89
+ :name => '',
90
+ :maildir => path,
91
+ :quota => @config[:mailbox_quota],
92
+ # :local_part => user,
93
+ }
94
+ domain.mailboxes << mailbox
95
+ domain.save or raise "Could not save Domain"
96
+ end
97
+
98
+ def add_alias(address, goto)
99
+ if Mailbox.exist?(address)
100
+ raise Error, "mailbox #{address} is already registered!"
101
+ end
102
+ if Alias.exist?(address)
103
+ raise Error, "alias #{address} is already registered!"
104
+ end
105
+ user, domain_name = address_split(address)
106
+ unless Domain.exist?(domain_name)
107
+ raise Error, "Invalid domain! #{domain_name}"
108
+ end
109
+ domain = Domain.find(domain_name)
110
+
111
+ new_alias = Alias.new
112
+ new_alias.attributes = {
113
+ :address => address,
114
+ :goto => goto,
115
+ }
116
+ domain.aliases << new_alias
117
+ domain.save or raise "Could not save Alias"
118
+ end
119
+
120
+ def delete_alias(address)
121
+ if Mailbox.exist?(address)
122
+ raise Error, "Can not delete mailbox by delete_alias. Use delete_account"
123
+ end
124
+ unless Alias.exist?(address)
125
+ raise Error, "#{address} is not found!"
126
+ end
127
+ Alias.all(:address => address).destroy or raise "Could not destroy Alias"
128
+ end
129
+
130
+ def add_domain(domain_name)
131
+ if domain_name !~ /.+\..+/
132
+ raise Error, "Ivalid domain! #{domain_name}"
133
+ end
134
+ if Domain.exist?(domain_name)
135
+ raise Error, "#{domain_name} is already registered!"
136
+ end
137
+ domain = Domain.new
138
+ domain.attributes = {
139
+ :domain_name => domain_name,
140
+ :description => domain_name,
141
+ :maxaliases => @config[:aliases],
142
+ :maxmailboxes => @config[:mailboxes],
143
+ :maxquota => @config[:maxquota],
144
+ }
145
+ domain.save or raise "Could not save Domain"
146
+ end
147
+
148
+ def delete_domain(domain_name)
149
+ unless Domain.exist?(domain_name)
150
+ raise Error, "Could not find domain #{domain_name}"
151
+ end
152
+
153
+ domain = Domain.find(domain_name)
154
+ domain.mailboxes.destroy or raise "Could not destroy Mailbox"
155
+ domain.aliases.destroy or raise "Could not destroy Alias"
156
+ domain.domain_admins.destroy or raise "Could not destroy DomainAdmin"
157
+ delete_unnecessary_admins
158
+
159
+ domain.destroy or raise "Could not destroy Domain"
160
+ end
161
+
162
+ def delete_admin(user_name)
163
+ unless Admin.exist?(user_name)
164
+ raise Error, "Could not find admin #{user_name}"
165
+ end
166
+ admin = Admin.find(user_name)
167
+ admin.domain_admins.destroy or raise "Could not destroy DomainAdmin"
168
+ admin.destroy or raise "Could not destroy Admin"
169
+ end
170
+
171
+ def delete_account(address)
172
+ unless Alias.exist?(address) && Mailbox.exist?(address)
173
+ raise Error, "Could not find account #{address}"
174
+ end
175
+
176
+ Mailbox.all(:username => address).destroy or raise "Could not destroy Mailbox"
177
+ Alias.all(:address => address).destroy or raise "Could not destroy Alias"
178
+ end
179
+
180
+ def delete_unnecessary_admins
181
+ Admin.unnecessary.destroy or raise "Could not destroy Admin"
182
+ end
183
+
184
+ def address_split(address)
185
+ address.split('@')
186
+ end
187
+ end
188
+ end
@@ -1,123 +1,288 @@
1
1
  require 'yaml'
2
2
  require 'postfix_admin'
3
3
 
4
- class PostfixAdmin
4
+ module PostfixAdmin
5
5
  class CLI
6
- CONFIG_FILE = '.postfix_admin.conf'
6
+ CONFIG_FILE = '~/.postfix_admin.conf'
7
7
  MIN_NUM_PASSWORD_CHARACTER = 5
8
8
 
9
9
  def initialize
10
10
  @config = load_config
11
- @admin = PostfixAdmin.new(@config)
11
+ @base = PostfixAdmin::Base.new(@config)
12
12
  end
13
- # ~/.postfix_adminrc
14
- # database: mysql://postfix:password@localhost/postfix
15
- # aliases: 30
16
- # mailboxes: 30
17
- # maxquota: 100
18
- def load_config
19
- unless File.exist?(config_file)
20
- create_config
21
- puts "configure file: #{config_file} was generated.\nPlease execute after edit it."
22
- exit
13
+
14
+ def show(domain)
15
+ show_summary(domain)
16
+
17
+ if domain
18
+ show_admin(domain)
19
+ show_address(domain)
20
+ show_alias(domain)
21
+ else
22
+ show_domain
23
+ show_admin
23
24
  end
24
- open(config_file) do |f|
25
- YAML.load(f.read)
25
+ end
26
+
27
+ def show_summary(domain_name=nil)
28
+ title = "Summary"
29
+ if domain_name
30
+ domain_check(domain_name)
31
+ title = "Summary of #{domain_name}"
32
+ end
33
+
34
+ report(title) do
35
+ if domain_name
36
+ domain = Domain.find(domain_name)
37
+ puts "Mailboxes : %4d" % domain.mailboxes.count
38
+ puts "Aliases : %4d" % domain.num_total_aliases
39
+ puts "Quota : %4d MB" % domain.maxquota
40
+ else
41
+ puts "Domains : %4d" % Domain.all_without_special_domain.count
42
+ puts "Admins : %4d" % Admin.count
43
+ puts "Mailboxes : %4d" % Mailbox.count
44
+ puts "Aliases : %4d" % Domain.num_total_aliases
45
+ end
26
46
  end
27
47
  end
28
- def create_config
29
- config = {
30
- 'database' => 'mysql://postfix:password@localhost/postfix',
31
- 'aliases' => 30,
32
- 'mailboxes' => 30,
33
- 'maxquota' => 100
34
- }
35
- open(config_file, 'w') do |f|
36
- f.write config.to_yaml
48
+
49
+ def setup_domain(domain, password)
50
+ admin = "admin@#{domain}"
51
+ add_domain(domain)
52
+ add_admin(admin, password)
53
+ add_admin_domain(admin, domain)
54
+ end
55
+
56
+ def show_domain
57
+ index = " No. Domain Aliases Mailboxes Quota (MB)"
58
+ report('Domains', index) do
59
+ if Domain.all_without_special_domain.count == 0
60
+ puts " No domains"
61
+ else
62
+ Domain.all_without_special_domain.each_with_index do |d, i|
63
+ puts "%4d %-20s %3d /%3d %3d /%3d %10d" %
64
+ [i+1, d.domain_name, d.num_total_aliases, d.maxaliases,
65
+ d.mailboxes.count, d.maxmailboxes, d.maxquota]
66
+ end
67
+ end
37
68
  end
38
- File.chmod(0600, config_file)
39
69
  end
40
- def config_file
41
- File.expand_path(CONFIG_FILE, ENV['HOME'])
70
+
71
+ def add_domain(domain)
72
+ if @base.add_domain(domain)
73
+ puts %Q!"#{domain}" was successfully registered.!
74
+ end
42
75
  end
43
- def print_line
44
- puts "-"*85
76
+
77
+ def super_admin(user_name, disable)
78
+ unless Admin.exist?(user_name)
79
+ raise Error, "Could not find admin #{user_name}"
80
+ end
81
+
82
+ if disable
83
+ Admin.find(user_name).super_admin = false
84
+ puts "Successfully disabled super admin flag of #{user_name}"
85
+ else
86
+ Admin.find(user_name).super_admin = true
87
+ puts "Successfully enabled super admin flag of #{user_name}"
88
+ end
45
89
  end
46
- def show_domain
47
- print_line
48
- puts " No. domain alias mail quota"
49
- print_line
50
- @admin.domains.each_with_index do |domain, i|
51
- printf("%4d %-20s %4d %4d %4d\n", i+1, domain.domain, domain.aliases, domain.mailboxes, domain.maxquota)
90
+
91
+ def change_admin_password(user_name, password)
92
+ change_password(Admin, user_name, password)
93
+ end
94
+
95
+ def change_account_password(user_name, password)
96
+ change_password(Mailbox, user_name, password)
97
+ end
98
+
99
+ def delete_domain(domain)
100
+ if @base.delete_domain(domain)
101
+ puts_deleted(domain)
52
102
  end
53
- print_line
54
103
  end
55
- def show_admin
56
- admins = @admin.admins
57
- if admins.count == 0
58
- puts "No admin in database\n"
59
- return
104
+
105
+ def show_admin(domain=nil)
106
+ admins = domain ? Admin.select{|a| a.has_domain?(domain)} : Admin.all
107
+ index = " No. Admin Domains Password"
108
+ report("Admins", index) do
109
+ if admins.count == 0
110
+ puts " No admins"
111
+ else
112
+ admins.each_with_index do |a, i|
113
+ domains = a.super_admin? ? 'Super admin' : a.domains.count
114
+ puts "%4d %-30s %11s %s" % [i+1, a.username, domains, a.password]
115
+ end
116
+ end
60
117
  end
61
- print_line
62
- puts " No. username password"
63
- print_line
64
- admins.each_with_index do |admin, i|
65
- printf("%4d %-20s %10s\n", i+1, admin.username, admin.password)
118
+
119
+ end
120
+
121
+ def show_address(domain)
122
+ domain_check(domain)
123
+
124
+ mailboxes = Domain.find(domain).mailboxes
125
+ index = " No. Email Quota (MB) Password"
126
+ report("Addresses", index) do
127
+ if mailboxes.count == 0
128
+ puts " No addresses"
129
+ else
130
+ mailboxes.each_with_index do |m, i|
131
+ quota = m.quota.to_f/1024000.0
132
+ puts "%4d %-30s %10.1f %s" % [i+1, m.username, quota, m.password]
133
+ end
134
+ end
66
135
  end
67
- print_line
136
+
68
137
  end
69
- def show_domain_account(domain)
70
- mailboxes = @admin.mailboxes(domain)
71
- if mailboxes.count == 0
72
- puts "No address in #{domain}\n"
73
- return
138
+
139
+ def show_alias(domain)
140
+ domain_check(domain)
141
+
142
+ aliases = Domain.find(domain).aliases.find_all do |mail_alias|
143
+ mail_alias.address != mail_alias.goto
74
144
  end
75
- print_line
76
- puts " No. address password quota(M) maildir"
77
- print_line
78
- mailboxes.each_with_index do |mailbox, i|
79
- printf("%4d %-20s %10s %7.1f %-30s\n", i+1, mailbox.username, mailbox.password, mailbox.quota.to_f/1024000.0, mailbox.maildir)
145
+
146
+ index = " No. Address Go to"
147
+ report("Aliases", index) do
148
+ if aliases.count == 0
149
+ puts " No aliases"
150
+ else
151
+ aliases.each_with_index do |a, i|
152
+ puts "%4d %-30s %s" % [i+1, a.address, a.goto]
153
+ end
154
+ end
80
155
  end
81
- print_line
156
+
82
157
  end
158
+
83
159
  def show_admin_domain(user_name)
84
- domain_admins = @admin.admin_domains(user_name)
85
- if domain_admins.count == 0
86
- puts "No domain in database\n"
160
+ admin = Admin.find(user_name)
161
+ if admin.domains.count == 0
162
+ puts "\nNo domain in database"
87
163
  return
88
164
  end
89
- print_line
90
- puts " No. domain"
91
- print_line
92
- domain_admins.each_with_index do |domain_admin, i|
93
- printf("%4d %-20s\n", i+1, domain_admin.domain)
165
+ report("Domains (#{user_name})", " No. Domain") do
166
+ admin.domains.each_with_index do |d, i|
167
+ puts "%4d %-20s" % [i+1, d.domain_name]
168
+ end
94
169
  end
95
- print_line
96
170
  end
97
- def add_domain(domain)
98
- @admin.add_domain(domain)
99
- end
100
- def add_admin(user_name, password)
171
+
172
+ def add_admin(user_name, password, super_admin=false)
101
173
  validate_password(password)
102
- @admin.add_admin(user_name, password)
174
+ if @base.add_admin(user_name, password)
175
+ if super_admin
176
+ Admin.find(user_name).super_admin = true
177
+ puts_registered(user_name, "a super admin")
178
+ else
179
+ puts_registered(user_name, "an admin")
180
+ end
181
+ end
182
+ end
183
+
184
+ def add_admin_domain(user_name, domain)
185
+ if @base.add_admin_domain(user_name, domain)
186
+ puts_registered(domain, "a domain of #{user_name}")
187
+ end
103
188
  end
189
+
104
190
  def add_account(address, password)
105
191
  validate_password(password)
106
- @admin.add_account(address, password)
192
+ if @base.add_account(address, password)
193
+ puts_registered(address, "an account")
194
+ end
107
195
  end
196
+
108
197
  def add_alias(address, goto)
109
- @admin.add_alias(address, goto)
198
+ if @base.add_alias(address, goto)
199
+ puts_registered("#{address}: #{goto}", "an alias")
200
+ end
110
201
  end
111
- def add_admin_domain(user_name, domain)
112
- @admin.add_admin_domain(user_name, domain)
202
+
203
+ def delete_alias(address)
204
+ puts_deleted(address) if @base.delete_alias(address)
113
205
  end
114
- def delete_domain(domain)
115
- @admin.delete_domain(domain)
206
+
207
+ def delete_admin(user_name)
208
+ puts_deleted(user_name) if @base.delete_admin(user_name)
209
+ end
210
+
211
+ def delete_account(address)
212
+ puts_deleted(address) if @base.delete_account(address)
213
+ end
214
+
215
+ private
216
+
217
+ def puts_registered(name, as_str)
218
+ puts %Q!"#{name}" was successfully registered as #{as_str}.!
219
+ end
220
+
221
+ def puts_deleted(name)
222
+ puts %Q!"#{name}" was successfully deleted.!
116
223
  end
224
+
225
+ def config_file
226
+ config_file = File.expand_path(CONFIG_FILE)
227
+ end
228
+
229
+ def load_config
230
+ unless File.exist?(config_file)
231
+ create_config(config_file)
232
+ puts "configure file: #{config_file} was generated.\nPlease execute after edit it."
233
+ exit
234
+ end
235
+ open(config_file) do |f|
236
+ YAML.load(f.read)
237
+ end
238
+ end
239
+
240
+ def create_config(config_file)
241
+ open(config_file, 'w') do |f|
242
+ f.write PostfixAdmin::Base::DEFAULT_CONFIG.to_yaml
243
+ end
244
+ File.chmod(0600, config_file)
245
+ end
246
+
247
+ def print_line
248
+ puts "-"*100
249
+ end
250
+
251
+ def report(title, index=nil)
252
+ puts "\n[#{title}]"
253
+ print_line if index
254
+ puts index if index
255
+ print_line
256
+ yield
257
+ print_line
258
+ end
259
+
260
+ def domain_check(domain_name)
261
+ unless Domain.exist?(domain_name)
262
+ raise Error, %Q!Could not find domain "#{domain_name}"!
263
+ end
264
+ end
265
+
117
266
  def validate_password(password)
118
267
  if password.size < MIN_NUM_PASSWORD_CHARACTER
119
- raise "Error: Password is too short. It should be larger than #{MIN_NUM_PASSWORD_CHARACTER}"
268
+ raise ArgumentError, "Password is too short. It should be larger than #{MIN_NUM_PASSWORD_CHARACTER}"
120
269
  end
121
270
  end
271
+
272
+ def change_password(klass, user_name, password)
273
+ unless klass.exist?(user_name)
274
+ raise Error, "Could not find #{user_name}"
275
+ end
276
+ validate_password(password)
277
+
278
+ obj = klass.find(user_name)
279
+ obj.password = password
280
+ if obj.save
281
+ puts "the password of #{user_name} was successfully changed."
282
+ else
283
+ raise "Could not change password of #{klass.name}"
284
+ end
285
+ end
286
+
122
287
  end
123
288
  end