interpreter: interpret `with` statement
[nit.git] / lib / privileges.nit
index d91a9a6..f17a28e 100644 (file)
@@ -47,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
 
@@ -71,8 +75,11 @@ class OptionUserAndGroup
 
        redef type VALUE: nullable UserGroup
 
-       init for_dropping_privileges do init("Drop privileges to user:group or simply user", "-u", "--usergroup")
-       init(help: String, names: String...) do super(help, null, names)
+       # Create an `OptionUserAndGroup` for dropping privileges
+       init for_dropping_privileges
+       do
+               init("Drop privileges to user:group or simply user", null, ["-u", "--usergroup"])
+       end
 
        redef fun convert(str)
        do
@@ -83,8 +90,7 @@ class OptionUserAndGroup
                        return new UserGroup(words[0], words[1])
                else
                        errors.add("Option {names.join(", ")} expected parameter in the format \"user:group\" or simply \"user\".\n")
-                       abort # FIXME only for nitc, remove and replace with next line when FFI is working in nitg
-                       #return null
+                       return null
                end
        end
 end