sys-admin 1.4.4 → 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES CHANGED
@@ -1,3 +1,13 @@
1
+ == 1.4.5 - 1-Mar-2009
2
+ * Added the User#groups method. This returns an array of groups that the
3
+ user belongs to. Suggestion inspired by Gonzalo Garramuno.
4
+ * Changed User#class to User#access_class to avoid conflicts with the
5
+ Ruby core Object method.
6
+ * Added more tests and renamed the test files.
7
+ * Removed an unnecessary function call where a platform might try to
8
+ get lastlog information even if the lastlog.h or utmp.h headers couldn't
9
+ be found.
10
+
1
11
  == 1.4.4 - 19-Nov-2008
2
12
  * Added the User#uid method for MS Windows (which is just the user's relative
3
13
  identifier).
data/README CHANGED
@@ -1,8 +1,11 @@
1
1
  == Description
2
- The sys-admin package is a unified, cross platform replacement for the
2
+ The sys-admin library is a unified, cross platform replacement for the
3
3
  Etc module.
4
4
 
5
5
  == Installation
6
+ = Typical Gem Installation
7
+ gem install sys-admin
8
+ = Local installation
6
9
  rake test (optional)
7
10
  rake install
8
11
 
@@ -167,6 +170,7 @@ Admin::Error < StandardError
167
170
  * Admin.delete_global_group
168
171
 
169
172
  Make the User and Group objects comparable.
173
+ Support BSD.
170
174
 
171
175
  == Known Bugs
172
176
  None that I'm aware of. If you find any, please log them on the project
@@ -176,7 +180,7 @@ Admin::Error < StandardError
176
180
  Ruby's
177
181
 
178
182
  == Copyright
179
- (C) 2005-2008, Daniel J. Berger
183
+ (C) 2005-2009, Daniel J. Berger
180
184
  All Rights Reserved
181
185
 
182
186
  == Author
data/Rakefile CHANGED
@@ -52,5 +52,10 @@ Rake::TestTask.new("test") do |t|
52
52
  t.libs << 'ext'
53
53
  t.libs.delete('lib')
54
54
  end
55
- t.test_files = FileList['test/tc_admin.rb']
55
+ t.libs << 'test'
56
+ t.test_files = FileList['test/test_sys_admin.rb']
57
+ end
58
+
59
+ task :test do
60
+ Rake.application[:clean].execute
56
61
  end
data/ext/sys/admin.c CHANGED
@@ -265,6 +265,37 @@ static VALUE admin_users(VALUE klass){
265
265
  );
266
266
  }
267
267
 
268
+ /* call-seq:
269
+ * User#groups # => ['staff', 'admin', ...]
270
+ *
271
+ * Returns an array of groups the user belongs to.
272
+ */
273
+ static VALUE user_groups(VALUE self){
274
+ VALUE v_groups, v_group, v_users, v_group_name, v_name, v_result;
275
+ int i;
276
+
277
+ v_name = rb_funcall(self, rb_intern("name"), 0, 0);
278
+ v_result = rb_ary_new();
279
+ v_groups = admin_groups(self);
280
+
281
+ /* Iterate over each group, checking its members. If the members includes
282
+ * the user name, we have a match.
283
+ */
284
+ if(!NIL_P(v_groups)){
285
+ for(i = 0; i < RARRAY(v_groups)->len; i++){
286
+ v_group = RARRAY(v_groups)->ptr[i];
287
+ v_users = rb_funcall(v_group, rb_intern("members"), 0, 0);
288
+
289
+ if(RTEST(rb_funcall(v_users, rb_intern("include?"), 1, v_name))){
290
+ v_group_name = rb_funcall(v_group, rb_intern("name"), 0, 0);
291
+ rb_ary_push(v_result, v_group_name);
292
+ }
293
+ }
294
+ }
295
+
296
+ return v_result;
297
+ }
298
+
268
299
  /*
269
300
  * The Sys::Admin class encapsulates typical operations surrounding the query
270
301
  * of user and group information.
@@ -296,6 +327,7 @@ void Init_admin(){
296
327
 
297
328
  /* Instance Methods */
298
329
  rb_define_method(cUser, "initialize", user_init, 0);
330
+ rb_define_method(cUser, "groups", user_groups, 0);
299
331
  rb_define_method(cGroup,"initialize", group_init, 0);
300
332
 
301
333
  /* User Attributes */
@@ -328,7 +360,7 @@ void Init_admin(){
328
360
  rb_define_attr(cUser, "age", 1, 1);
329
361
 
330
362
  /* The user's access class */
331
- rb_define_attr(cUser, "class", 1, 1);
363
+ rb_define_attr(cUser, "access_class", 1, 1);
332
364
 
333
365
  /* Another comment field. Rarely used. */
334
366
  rb_define_attr(cUser, "comment", 1, 1);
@@ -336,7 +368,7 @@ void Init_admin(){
336
368
  /* Account expiration date */
337
369
  rb_define_attr(cUser, "expire", 1, 1);
338
370
 
339
- /* Next data a password change will be needed */
371
+ /* Next date a password change will be needed */
340
372
  rb_define_attr(cUser, "change", 1, 1);
341
373
 
342
374
  /* The last time the user logged in */
@@ -364,6 +396,6 @@ void Init_admin(){
364
396
 
365
397
  /* Constants */
366
398
 
367
- /* 1.4.4: The version of this library */
399
+ /* 1.4.5: The version of this library */
368
400
  rb_define_const(cAdmin, "VERSION", rb_str_new2(SYS_ADMIN_VERSION));
369
401
  }
data/ext/sys/admin.h CHANGED
@@ -8,7 +8,7 @@
8
8
  #include <errno.h>
9
9
  #include <string.h>
10
10
 
11
- #define SYS_ADMIN_VERSION "1.4.4"
11
+ #define SYS_ADMIN_VERSION "1.4.5"
12
12
 
13
13
  #ifdef HAVE_LASTLOG_H
14
14
  #include <lastlog.h>
@@ -226,7 +226,7 @@ static VALUE get_user(struct passwd* pwd){
226
226
  #endif
227
227
 
228
228
  #ifdef HAVE_ST_PW_CLASS
229
- rb_iv_set(v_user, "@class", rb_str_new2(pwd->pw_class));
229
+ rb_iv_set(v_user, "@access_class", rb_str_new2(pwd->pw_class));
230
230
  #endif
231
231
 
232
232
  #ifdef HAVE_ST_PW_EXPIRE
@@ -237,8 +237,9 @@ static VALUE get_user(struct passwd* pwd){
237
237
  rb_iv_set(v_user, "@change", rb_time_new(pwd->pw_change, 0));
238
238
  #endif
239
239
 
240
- /* Get the lastlog info for the given user */
240
+ #if defined(HAVE_LASTLOG_H) || defined(HAVE_UTMP_H)
241
241
  get_lastlog_info(pwd, v_user);
242
+ #endif
242
243
 
243
244
  return v_user;
244
245
  }
@@ -313,7 +314,7 @@ void get_user_from_value(VALUE v_user, struct passwd* pwd){
313
314
  #endif
314
315
 
315
316
  #ifdef HAVE_ST_PW_CLASS
316
- VALUE v_class = rb_iv_get(v_user, "@class");
317
+ VALUE v_class = rb_iv_get(v_user, "@access_class");
317
318
  if(!NIL_P(v_class)){
318
319
  SafeStringValue(v_class);
319
320
  pwd->pw_class = StringValuePtr(v_class);
@@ -408,6 +409,9 @@ void get_group_from_value(VALUE v_group, struct group* grp){
408
409
  * :no-doc:
409
410
  *
410
411
  * Helper function that gets lastlog information for the User object.
412
+ *--
413
+ * Note that even if the platform supports lastlog information, it can
414
+ * still be empty or nil.
411
415
  */
412
416
  int get_lastlog_info(struct passwd* pwd, VALUE v_user){
413
417
  int fd;
@@ -1,14 +1,21 @@
1
1
  ###############################################################################
2
- # tc_admin.rb
2
+ # test_sys_admin.rb
3
3
  #
4
4
  # This exists mostly for the sake of the gemspec, so that it calls the right
5
5
  # test suite based on the platform.
6
6
  ###############################################################################
7
- $LOAD_PATH.unshift Dir.pwd
8
- $LOAD_PATH.unshift File.join(Dir.pwd, 'test')
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+ require 'test/unit'
9
10
 
10
11
  if File::ALT_SEPARATOR
11
- require 'tc_windows'
12
+ require 'test_sys_admin_windows'
12
13
  else
13
- require 'tc_unix'
14
+ require 'test_sys_admin_unix'
15
+ end
16
+
17
+ class TC_Sys_Admin_All < Test::Unit::TestCase
18
+ def test_version
19
+ assert_equal('1.4.5', Sys::Admin::VERSION)
20
+ end
14
21
  end
@@ -0,0 +1,246 @@
1
+ ###############################################################################
2
+ # test_sys_admin_unix.rb
3
+ #
4
+ # Test suite for the Unix version of sys-admin. This test should be run
5
+ # via the 'rake test' task.
6
+ ###############################################################################
7
+ require 'test/unit'
8
+ require 'sys/admin'
9
+ include Sys
10
+
11
+ class TC_Sys_Admin_Unix < Test::Unit::TestCase
12
+ def setup
13
+ @user = 'nobody'
14
+ @user_id = 0
15
+
16
+ @group = 'sys'
17
+ @group_id = 3
18
+ end
19
+
20
+ ## Admin singleton methods
21
+
22
+ def test_get_login
23
+ assert_respond_to(Admin, :get_login)
24
+ assert_nothing_raised{ Admin.get_login }
25
+ assert_kind_of(String, Admin.get_login)
26
+ end
27
+
28
+ def test_get_user_basic
29
+ assert_respond_to(Admin, :get_user)
30
+ assert_nothing_raised{ Admin.get_user(@user) }
31
+ assert_nothing_raised{ Admin.get_user(@user_id) }
32
+ end
33
+
34
+ def test_get_user_by_name
35
+ assert_kind_of(User, Admin.get_user(@user))
36
+ end
37
+
38
+ def test_get_user_by_id
39
+ assert_kind_of(User, Admin.get_user(@user_id))
40
+ end
41
+
42
+ def test_get_user_expected_errors
43
+ assert_raise(ArgumentError){ Admin.get_user }
44
+ assert_raise(TypeError){ Admin.get_user([]) }
45
+ assert_raise(Admin::Error){ Admin.get_user('foofoofoo') }
46
+ end
47
+
48
+ def test_users_basic
49
+ assert_respond_to(Admin, :users)
50
+ assert_nothing_raised{ Admin.users }
51
+ end
52
+
53
+ def test_users
54
+ assert_kind_of(Array, Admin.users)
55
+ assert_kind_of(User, Admin.users.first)
56
+ end
57
+
58
+ def test_users_block_form
59
+ array = []
60
+ assert_nothing_raised{ Admin.users{ |g| array << g } }
61
+ assert_equal(true, Admin.users.length == array.length)
62
+ assert_nothing_raised{ Admin.users{ |g| break } }
63
+ end
64
+
65
+ def test_users_expected_errors
66
+ assert_raise(ArgumentError){ Admin.users(@user_id) }
67
+ end
68
+
69
+ def test_get_group_basic
70
+ assert_respond_to(Admin, :get_group)
71
+ assert_nothing_raised{ Admin.get_group(@group) }
72
+ assert_nothing_raised{ Admin.get_group(@group_id) }
73
+ end
74
+
75
+ def test_get_group_by_name
76
+ assert_kind_of(Group, Admin.get_group(@group))
77
+ end
78
+
79
+ def test_get_group_by_id
80
+ assert_kind_of(Group, Admin.get_group(@group_id))
81
+ end
82
+
83
+ def test_get_group_expected_errors
84
+ assert_raise(ArgumentError){ Admin.get_group }
85
+ assert_raise(TypeError){ Admin.get_group([]) }
86
+ assert_raise(Admin::Error){ Admin.get_group('foofoofoo') }
87
+ end
88
+
89
+ def test_groups_basic
90
+ assert_respond_to(Admin, :groups)
91
+ assert_nothing_raised{ Admin.groups }
92
+ end
93
+
94
+ def test_groups
95
+ assert_kind_of(Array, Admin.groups)
96
+ assert_kind_of(Group, Admin.groups.first)
97
+ end
98
+
99
+ def test_groups_expected_errors
100
+ assert_raise(ArgumentError){ Admin.groups(@group_id) }
101
+ end
102
+
103
+ def test_groups_block_form
104
+ array = []
105
+ assert_nothing_raised{ Admin.groups{ |g| array << g } }
106
+ assert_equal(true, Admin.groups.length == array.length)
107
+ assert_nothing_raised{ Admin.groups{ |g| break } }
108
+ end
109
+
110
+ ## User Tests
111
+
112
+ def test_user_name
113
+ @user = Admin.users.first
114
+ assert_respond_to(@user, :name)
115
+ assert_kind_of(String, @user.name)
116
+ end
117
+
118
+ def test_user_passwd
119
+ @user = Admin.users.first
120
+ assert_respond_to(@user, :passwd)
121
+ assert_kind_of(String, @user.passwd)
122
+ end
123
+
124
+ def test_user_uid
125
+ @user = Admin.users.first
126
+ assert_respond_to(@user, :uid)
127
+ assert_kind_of(Fixnum, @user.uid)
128
+ end
129
+
130
+ def test_user_gid
131
+ @user = Admin.users.first
132
+ assert_respond_to(@user, :gid)
133
+ assert_kind_of(Fixnum, @user.gid)
134
+ end
135
+
136
+ def test_user_dir
137
+ @user = Admin.users.first
138
+ assert_respond_to(@user, :dir)
139
+ assert_kind_of(String, @user.dir)
140
+ end
141
+
142
+ def test_user_shell
143
+ @user = Admin.users.first
144
+ assert_respond_to(@user, :shell)
145
+ assert_kind_of(String, @user.shell)
146
+ end
147
+
148
+ def test_user_gecos
149
+ @user = Admin.users.first
150
+ assert_respond_to(@user, :gecos)
151
+ assert_kind_of(String, @user.gecos)
152
+ end
153
+
154
+ def test_user_quota
155
+ @user = Admin.users.first
156
+ assert_respond_to(@user, :quota)
157
+ assert_true([Fixnum, NilClass].include?(@user.quota.class))
158
+ end
159
+
160
+ def test_user_age
161
+ @user = Admin.users.first
162
+ assert_respond_to(@user, :age)
163
+ assert_true([Fixnum, NilClass].include?(@user.age.class))
164
+ end
165
+
166
+ def test_user_access_class
167
+ @user = Admin.users.first
168
+ assert_respond_to(@user, :access_class)
169
+ assert_true([String, NilClass].include?(@user.access_class.class))
170
+ end
171
+
172
+ def test_user_comment
173
+ @user = Admin.users.first
174
+ assert_respond_to(@user, :comment)
175
+ assert_true([String, NilClass].include?(@user.comment.class))
176
+ end
177
+
178
+ def test_user_expire
179
+ @user = Admin.users.first
180
+ assert_respond_to(@user, :expire)
181
+ assert_true([Time, NilClass].include?(@user.expire.class))
182
+ end
183
+
184
+ def test_user_change
185
+ @user = Admin.users.first
186
+ assert_respond_to(@user, :change)
187
+ assert_true([Time, NilClass].include?(@user.change.class))
188
+ end
189
+
190
+ def test_user_login_time
191
+ @user = Admin.users.first
192
+ assert_respond_to(@user, :login_time)
193
+ assert_true([Time, NilClass].include?(@user.login_time.class))
194
+ end
195
+
196
+ def test_user_login_device
197
+ @user = Admin.users.first
198
+ assert_respond_to(@user, :login_device)
199
+ assert_true([String, NilClass].include?(@user.login_device.class))
200
+ end
201
+
202
+ def test_user_login_host
203
+ @user = Admin.users.first
204
+ assert_respond_to(@user, :login_host)
205
+ assert_true([String, NilClass].include?(@user.login_host.class))
206
+ end
207
+
208
+ def test_user_groups
209
+ @user = Admin.users.first
210
+ assert_respond_to(@user, :groups)
211
+ assert_kind_of(Array, @user.groups)
212
+ end
213
+
214
+ ## Group Tests
215
+
216
+ def test_group_name
217
+ @group = Admin.groups.first
218
+ assert_respond_to(@group, :name)
219
+ assert_kind_of(String, @group.name)
220
+ end
221
+
222
+ def test_group_gid
223
+ @group = Admin.groups.first
224
+ assert_respond_to(@group, :gid)
225
+ assert_kind_of(Fixnum, @group.gid)
226
+ end
227
+
228
+ def test_group_members
229
+ @group = Admin.groups.first
230
+ assert_respond_to(@group, :members)
231
+ assert_kind_of(Array, @group.members)
232
+ end
233
+
234
+ def test_group_passwd
235
+ @group = Admin.groups.first
236
+ assert_respond_to(@group, :passwd)
237
+ assert_kind_of(String, @group.passwd)
238
+ end
239
+
240
+ def teardown
241
+ @user = nil
242
+ @user_id = nil
243
+ @group = nil
244
+ @group_id = nil
245
+ end
246
+ end
@@ -1,5 +1,5 @@
1
1
  ###############################################################################
2
- # tc_windows.rb
2
+ # test_sys_admin_windows.rb
3
3
  #
4
4
  # Test suite for the Win32 version of sys-admin. Note that some of the tests
5
5
  # are numbered to ensure a certain order. That way I can add test users
@@ -29,10 +29,6 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
29
29
  @group_id = 546 # best guess, may fail
30
30
  end
31
31
 
32
- def test_version
33
- assert_equal('1.4.4', Admin::VERSION)
34
- end
35
-
36
32
  def test_01_add_local_user
37
33
  assert_respond_to(Admin, :add_local_user)
38
34
  assert_nothing_raised{ Admin.add_local_user('foo') }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sys-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-19 00:00:00 -07:00
12
+ date: 2009-01-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: A unified, cross platform replacement for the "etc" package.
16
+ description: A unified, cross platform replacement for the "etc" library.
17
17
  email: djberg96@gmail.com
18
18
  executables: []
19
19
 
@@ -27,15 +27,15 @@ extra_rdoc_files:
27
27
  files:
28
28
  - doc/sys-admin-unix.txt
29
29
  - doc/sys-admin-windows.txt
30
- - examples/groups.rb
31
30
  - examples/users.rb
32
- - test/tc_admin.rb
33
- - test/tc_unix.rb
34
- - test/tc_windows.rb
35
- - CHANGES
31
+ - examples/groups.rb
32
+ - test/test_sys_admin_windows.rb
33
+ - test/test_sys_admin.rb
34
+ - test/test_sys_admin_unix.rb
36
35
  - MANIFEST
37
- - Rakefile
38
36
  - README
37
+ - CHANGES
38
+ - Rakefile
39
39
  - ext/sys/admin.c
40
40
  - ext/sys/admin.h
41
41
  has_rdoc: true
@@ -60,9 +60,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  requirements: []
61
61
 
62
62
  rubyforge_project: sysutils
63
- rubygems_version: 1.2.0
63
+ rubygems_version: 1.3.1
64
64
  signing_key:
65
65
  specification_version: 2
66
- summary: A unified, cross platform replacement for the "etc" package.
66
+ summary: A unified, cross platform replacement for the "etc" library.
67
67
  test_files:
68
- - test/tc_admin.rb
68
+ - test/test_sys_admin.rb
data/test/tc_unix.rb DELETED
@@ -1,81 +0,0 @@
1
- ###############################################################################
2
- # tc_unix.rb
3
- #
4
- # Test suite for the Unix version of sys-admin. This test should be run
5
- # via the 'rake test' task.
6
- ###############################################################################
7
- require 'test/unit'
8
- require 'sys/admin'
9
- include Sys
10
-
11
- class TC_Sys_Admin_Unix < Test::Unit::TestCase
12
- def setup
13
- @user = "nobody"
14
- @userid = 0
15
- @group = "sys"
16
- @groupid = 3
17
- end
18
-
19
- def test_version
20
- assert_equal('1.4.4', Admin::VERSION)
21
- end
22
-
23
- def test_get_login
24
- assert_respond_to(Admin, :get_login)
25
- assert_nothing_raised{ Admin.get_login }
26
- assert_kind_of(String, Admin.get_login)
27
- end
28
-
29
- def test_get_user
30
- assert_respond_to(Admin, :get_user)
31
- assert_nothing_raised{ Admin.get_user(@user) }
32
- assert_nothing_raised{ Admin.get_user(@userid) }
33
- assert_kind_of(User, Admin.get_user(@user))
34
- assert_kind_of(User, Admin.get_user(@userid))
35
- assert_raises(Admin::Error){ Admin.get_user("foofoo") }
36
- end
37
-
38
- def test_users
39
- assert_respond_to(Admin, :users)
40
- assert_nothing_raised{ Admin.users }
41
- assert_kind_of(Array, Admin.users)
42
- assert_kind_of(User, Admin.users.first)
43
- end
44
-
45
- def test_users_block_form
46
- array = []
47
- assert_nothing_raised{ Admin.users{ |g| array << g } }
48
- assert_equal(true, Admin.users.length == array.length)
49
- assert_nothing_raised{ Admin.users{ |g| break } }
50
- end
51
-
52
- def test_get_group
53
- assert_respond_to(Admin, :get_group)
54
- assert_nothing_raised{ Admin.get_group(@group) }
55
- assert_nothing_raised{ Admin.get_group(@groupid) }
56
- assert_kind_of(Group, Admin.get_group(@group))
57
- assert_kind_of(Group, Admin.get_group(@groupid))
58
- assert_raises(Admin::Error){ Admin.get_group("foofoo") }
59
- end
60
-
61
- def test_groups
62
- assert_respond_to(Admin, :groups)
63
- assert_nothing_raised{ Admin.groups }
64
- assert_kind_of(Array, Admin.groups)
65
- assert_kind_of(Group, Admin.groups.first)
66
- end
67
-
68
- def test_groups_block_form
69
- array = []
70
- assert_nothing_raised{ Admin.groups{ |g| array << g } }
71
- assert_equal(true, Admin.groups.length == array.length)
72
- assert_nothing_raised{ Admin.groups{ |g| break } }
73
- end
74
-
75
- def teardown
76
- @user = nil
77
- @userid = nil
78
- @group = nil
79
- @groupid = nil
80
- end
81
- end