File Coverage

blib/lib/Net/Gnats/Command/USER.pm
Criterion Covered Total %
statement 30 34 88.2
branch 10 12 83.3
condition n/a
subroutine 9 9 100.0
pod 2 4 50.0
total 51 59 86.4


line stmt bran cond sub pod time code
1             package Net::Gnats::Command::USER;
2 40     40   198 use parent 'Net::Gnats::Command';
  40         87  
  40         218  
3 40     40   2304 use strictures;
  40         66  
  40         228  
4             BEGIN {
5 40     40   3464 $Net::Gnats::Command::USER::VERSION = '0.21';
6             }
7 40     40   204 use vars qw($VERSION);
  40         63  
  40         1801  
8              
9 40     40   213 use Net::Gnats::Constants qw(CODE_INFORMATION CODE_NO_ACCESS CODE_OK);
  40         71  
  40         15967  
10              
11             =head1 NAME
12              
13             Net::Gnats::Command::USER
14              
15             =head1 DESCRIPTION
16              
17             Specifies the userid and password for database access. Either both a
18             username and password must be specified, or they both may be
19             omitted; in the latter case, the current access level is returned.
20              
21             =head1 PROTOCOL
22              
23             USER
24              
25             =head1 RESPONSES
26              
27             The possible server responses are:
28              
29             350 (CODE_INFORMATION) The current access level is specified.
30              
31             422 (CODE_NO_ACCESS) A matching username and password could not be
32             found.
33              
34             210 (CODE_OK) A matching username and password was found, and the
35             login was successful.
36              
37             =cut
38              
39             my $c = 'USER';
40              
41             sub new {
42 85     85 1 286 my ( $class, %options ) = @_;
43              
44 85         307 my $self = bless \%options, $class;
45 85         446 return $self;
46             }
47              
48             sub as_string {
49 169     169 1 209 my ( $self ) = @_;
50 169 100       994 return $c if not defined $self->{username};
51 81 100       4350 return undef if not defined $self->{password};
52 80         437 return $c . ' ' . $self->{username} . ' ' . $self->{password};
53             }
54              
55             sub level {
56 43     43 0 101 my ($self) = @_;
57             # get response. if username is specified, we will get a database
58             # for the first content string. if not specified, we simply get the
59             # level from the second result.
60              
61             # Examples:
62             # USER madmin madmin
63             # 210-Now accessing GNATS database 'default'
64             # 210 User access level set to 'admin'
65              
66             # user
67             # 351-The current user access level is:
68             # 350 admin
69              
70 43 50       189 if ( defined $self->{username} ) {
71 0         0 $self->{db} = @{$self->response->as_list}[0] =~ /Now accessing GNATS database '(.*)'/;
  0         0  
72 0         0 $self->{level} = @{$self->response->as_list}[1] =~ /User access level set to '(.*)'/;
  0         0  
73             }
74             else {
75 43         85 $self->{level} = @{$self->response->as_list}[1];
  43         152  
76             }
77 43         188 return $self->{level};
78             }
79              
80             sub is_ok {
81 156     156 0 262 my ($self) = @_;
82 156 100       419 return 0 if not defined $self->response;
83 155 50       382 return 0 if not defined $self->response->code;
84 155 100       407 return 0 if $self->response->code == CODE_NO_ACCESS;
85 154         841 return 1;
86             }
87              
88             1;