> -----Original Message-----
> From: David vd Geer Inhuur tbv IPlib
> [mailto:[EMAIL PROTECTED]]
> Sent: Monday, June 24, 2002 6:03 AM
> To: [EMAIL PROTECTED]
> Subject: if-statement and grep in one go
> 
> 
> 
> Hi,
> 
> I need some help on the following.
> In my script I show users some infodocs after they are 
> verified to be valid users.
> Users can have the permissions following the ruleset: 
> - user (all perm)
> - group (all perm), 
> - descr (Not allowed to actualy open this document).
> 
> Now I do the following in my script :
> 
> # -------------
>  open(FILE, "< ${dir}/user.perm");
>  my @users = <FILE>;
>  close FILE;
>  chomp (@users);
> 
>  my @out = grep {/$pwuser/} @users;
>  my @out1 = grep {/$group/} @users;
>  if ((!(@out)) && (!(@out1))) { print "Sorry $pwuser you have 
> no acces to this IP-Block"; exit; }
> # --------------
> 
> But if a user with description access enters the complete 
> link right now,
> he can view the entire document.
> Anyway, How do I build in an if-statement in here ??
> I know I can change the foreach loop and build the if 
> statement within the loop,
> but isn't there something like :
> 
> my @out = if (m/user:/) { grep {/$pwuser/} } @users;  ## This 
> doesn't work
> my @out1 = if (m/group:/) { grep {/$group/} } @users;  ## 
> This doesn't work
> 
> an example of the file user.perm would be :

I think you need to parse this file into some structures rather
than using the simple regex approach. Even if you check only the
user: line, your logic would allow user names like 'user', or 'v',
or ',' or even ''.

I would do something like this:

  my (%u, %g);
  open(FILE, "< ${dir}/user.perm") or die $!;
  while my $line (<FILE>) {
      if ($line =~ /^user:/) {
         $u{$_} = 1 for split(/\s*,\s*/, substr($line, 5));
      }
      if ($line =~ /^group:/) {
         $g{$_} = 1 for split(/\s*,\s*/, substr($line, 6));
      }
  }
  close(FILE);

Now you can check for a valid user/group like this:

   print "Invalid login" unless $u{$user} && $g{$group};

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to