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   220 use parent 'Net::Gnats::Command';
  40         60  
  40         187  
3 40     40   2063 use strictures;
  40         60  
  40         198  
4             BEGIN {
5 40     40   3064 $Net::Gnats::Command::USER::VERSION = '0.20';
6             }
7 40     40   183 use vars qw($VERSION);
  40         51  
  40         1490  
8              
9 40     40   187 use Net::Gnats::Constants qw(CODE_INFORMATION CODE_NO_ACCESS CODE_OK);
  40         64  
  40         13580  
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 203 my ( $class, %options ) = @_;
43              
44 85         285 my $self = bless \%options, $class;
45 85         372 return $self;
46             }
47              
48             sub as_string {
49 169     169 1 216 my ( $self ) = @_;
50 169 100       883 return $c if not defined $self->{username};
51 81 100       2999 return undef if not defined $self->{password};
52 80         367 return $c . ' ' . $self->{username} . ' ' . $self->{password};
53             }
54              
55             sub level {
56 43     43 0 80 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       161 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         76 $self->{level} = @{$self->response->as_list}[1];
  43         126  
76             }
77 43         218 return $self->{level};
78             }
79              
80             sub is_ok {
81 156     156 0 211 my ($self) = @_;
82 156 100       348 return 0 if not defined $self->response;
83 155 50       334 return 0 if not defined $self->response->code;
84 155 100       352 return 0 if $self->response->code == CODE_NO_ACCESS;
85 154         687 return 1;
86             }
87              
88             1;