-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.pm
More file actions
178 lines (146 loc) · 2.92 KB
/
User.pm
File metadata and controls
178 lines (146 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- Mode: perl; indent-tabs-mode: nil -*-
# Copyright(c) 2003-2007 Robert L. Brown. This is licensed software
# only to be used with an explicit right-to-use license from the
# copyright holder.
package User;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
require Exporter;
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(); # Symbols to export on request
use CGI qw(:standard *table *ol *ul *Tr *td escape *p);
use Login;
use Database;
use Argcvt;
use Layout;
use Utility;
use Password;
use UserTable;
my $metaData = \%::UserTable;
sub getTable
{
return $metaData;
}
sub getTableName
{
return $metaData->{'table'};
}
##
sub addConverter {
my $cvtlist = shift;
$cvtlist->{'user_id'} = \&User::convert;
$cvtlist->{'candidate.owner_id'} = \&User::convert;
}
sub convert
{
my $value = shift;
return User::getName($value);
}
##
## Caching routines
##
my %cache;
##
## Note: this can either take a record ref or a PK id
##
sub getName
{
my $arg = shift;
my $rec;
if ( ref($arg) eq "HASH" ) {
$rec = $arg;
} else {
$rec = User::getRecord($arg);
}
if ( $rec ) {
return $rec->{'name'};
} else {
return "anonymous";
}
}
sub getNamesLike
{
my $str = shift;
if ( !$str ) {
return undef;
}
my $where = "name LIKE " . SQLQuote("$str");
return Database::getRecordsWhere({
-table=>\%::UserTable,
-where=>$where,
});
}
sub getRecord
{
my $id = shift;
if ( $id ) {
if ( !exists $cache{$id} ) {
my $user = getRecordById({
-table=>\%::UserTable,
-id=>$id,
});
$cache{$id} = $user;
} else {
Database::addQueryComment("cache hit User $id");
}
return $cache{$id};
} else {
return undef;
}
}
sub getRecordByName
{
my $name = shift;
my @records = Database::getRecordsMatch({
-table=>User::getTable(),
-column=>"name",
-value=>$name,
});
if ( scalar @records == 0 ) {
return undef;
} else {
return $records[0];
}
}
sub getRecordBy
{
my $argv = shift;
argcvt($argv, ["column","value"]);
my @records = Database::getRecordsMatch({
-table => User::getTable(),
-column => $argv->{'column'},
-value => $argv->{'value'},
});
if ( scalar @records == 0 ) {
return undef;
} else {
return $records[0];
}
}
sub isActive
{
my $id = shift;
my $rec = User::getRecord($id);
return $rec->{'active'} eq 'Y';
}
sub isActiveSQL
{
return "user.active = " . SQLQuote("Y");
}
sub cryptPassword
{
return Password::encrypt(@_);
}
sub matchPassword
{
return Password::match(@_);
}
sub getAll
{
return Database::getAllRecords({
-table=>$metaData,
});
}
1;