lib: move some examples/* into specific subdirectories of their lib
[nit.git] / lib / privileges.nit
index e3e1220..7a5503f 100644 (file)
@@ -22,6 +22,22 @@ module privileges
 
 import opts
 
+redef class Text
+       # Does the operating system know the user named `self`?
+       fun user_exists: Bool
+       do
+               var passwd = new Passwd.from_name(to_s)
+               return not passwd.address_is_null
+       end
+
+       # Does the operating system know the group named `self`?
+       fun group_exists: Bool
+       do
+               var passwd = new Group.from_name(to_s)
+               return not passwd.address_is_null
+       end
+end
+
 # Class to manage user groups
 class UserGroup
 
@@ -31,16 +47,20 @@ class UserGroup
        # Group name
        var group: nullable String
 
-       # Drop privileges of a user and set his privileges back to default (program privileges)
+       # Drop privileges of the running program to those of `self`
+       #
+       # require: `user.user_exists and (group == null or group.group_exists)`
        fun drop_privileges
        do
                var passwd = new Passwd.from_name(user)
+               assert not passwd.address_is_null
                var uid = passwd.uid
 
                var group = group
                var gid
                if group != null then
                        var gpasswd = new Group.from_name(group)
+                       assert not gpasswd.address_is_null
                        gid = gpasswd.gid
                else gid = passwd.gid