/
Admin.pm
280 lines (207 loc) · 7.38 KB
/
Admin.pm
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package LedgerSMB::DBObject::Admin;
use base LedgerSMB::DBObject;
use LedgerSMB::Location;
use LedgerSMB::DBObject::Employee;
use LedgerSMB::Contact;
use LedgerSMB::DBObject::Employee;
#[18:00:31] <aurynn> I'd like to split them employee/user and roles/prefs
#[18:00:44] <aurynn> edit/create employee and add user features if needed.
sub save {
$self->error("Cannot save an Adminstrator object.");
}
sub save_user {
my $self = shift @_;
my $entity = LedgerSMB::DBObject::Employee->new(base=>$self, copy=>'none');
$entity->set(name=>$self->{first_name}." ".$self->{last_name});
if (!defined $self->{first_name} or !defined $self->{last_name}){
return;
}
$entity->save();
$self->{entity_id} = $entity->{entity};
my ($user_id) = $self->exec_method( funcname => "admin__save_user" );
$self->{user_id} = $user_id->{admin__save_user};
my $employee = LedgerSMB::DBObject::Employee->new( base=>$self, copy=>'list',
merge=>[
'salutation',
'first_name',
'last_name',
'employeenumber',
]
);
$employee->{entity_id} = $entity_id->{id};
$employee->save();
# now, check for user-specific stuff. Is this person a user or not?
my $user = LedgerSMB::DBObject::User->new(base=>$self, copy=>'list',
merge=>[
'username',
'password',
'is_a_user',
'user_id',
]
);
$user->get();
$self->{user} = $user;
$self->{employee} = $employee;
$self->debug({file => '/tmp/user11'});
if ($self->{password}){
return;
}
my $loc = LedgerSMB::DBObject::Location->new(base=>$self, copy=>'list',
merge=>[
'address1',
'address2',
'city',
'state',
'zipcode',
'country',
'companyname',
]
);
$loc->save();
$employee->set_location($loc->{id});
$loc->(person=>$employee);
my $workphone = LedgerSMB::Contact->new(base=>$self);
my $homephone = LedgerSMB::Contact->new(base=>$self);
my $email = LedgerSMB::Contact->new(base=>$self);
$workphone->set(person=>$employee, class=>1, contact=>$self->{workphone});
$homephone->set(person=>$employee, class=>11, contact=>$self->{homephone});
$email->set(person=>$employee, class=>12, contact=>$self->{email});
$workphone->save();
$homephone->save();
$email->save();
}
sub save_roles {
my $self = shift @_;
my $user = LedgerSMB::DBObject::User->new( base=>$self, copy=>'all' );
$user->get();
$self->{modifying_user} = $user->{user}->{username};
my @roles = $self->exec_method( funcname => "admin__get_roles" );
my @user_roles = $self->exec_method(funcname => "admin__get_roles_for_user");
my %active_roles;
for my $role (@user_roles) {
# These are our user's roles.
print STDERR "Have $role->{rolname}\n";
$active_roles{"$role->{rolname}"} = 1;
}
my $status;
for my $r ( @roles) {
my $role = $r->{rolname};
my $reqrole = $role;
$reqrole =~ s/^lsmb_$self->{company}__//;
# These roles are were ALL checked on the page, so they're the active ones.
if ( $active_roles{$role} && $self->{$reqrole} ) {
# do nothing.
;
}
elsif ($active_roles{$role} && !($self->{$reqrole} )) {
# do remove function
$status = $self->call_procedure(procname => "admin__remove_user_from_role",
args=>[ $self->{ modifying_user }, $role ] );
}
elsif ($self->{$reqrole} and !($active_roles{$role} )) {
# do add function
$status = $self->call_procedure(procname => "admin__add_user_to_role",
args=>[ $self->{ modifying_user }, $role ]
);
}
}
$self->{dbh}->commit;
}
sub save_group {
my $self = shift @_;
my $existant = shift @{ $self->call_procedure (procname=> "is_group", args=>[$self->{modifying_group}]) };
my $group = shift @{ $self->exec_method (funcname=> "save_group") };
# first we grab all roles
my $roles = $self->call_procedure( procname => "admin__all_roles" );
my $user_roles = $self->call_procedure(procname => "admin__get_user_roles",
args=>[ $self->{ group_name } ]
);
my %active_roles;
for my $role (@{$user_roles}) {
# These are our user's roles.
$active_roles{$role} = 1;
}
my $status;
for my $role ( @{ $roles } ) {
# These roles are were ALL checked on the page, so they're the active ones.
if ($active_roles{$role} && $self->{incoming_roles}->{$role}) {
# we don't need to do anything.
}
elsif ($active_roles{$role} && !($self->{incoming_roles}->{$role} )) {
# do remove function
$status = $self->exec_method(
procname => "admin__remove_group_from_role",
args=>[ $self->{ modifying_user }, $role ]
);
}
elsif ($self->{incoming_roles}->{$role} and !($active_roles{$role} )) {
# do add function
$status = $self->exec_method(
procname => "admin__add_group_to_role",
args=>[ $self->{ modifying_user }, $role ]
);
}
}
}
sub delete_user {
my $self = shift @_;
my $status = shift @{ $self->exec_method(procname=>'admin__delete_user',
args=>[$self->{modifying_user}])
};
if ($status) {
return 1;
} else {
$self->error('Delete user failed.');
#my $error = LedgerSMB::Error->new("Delete user failed.");
#$error->set_status($status);
#return $error;
}
}
sub delete_group {
my $self = shift @_;
my $status = shift @{ $self->exec_method(procname=>'admin__delete_group',
args=>[$self->{groupname}]) }
;
if ($status) {
return 1;
} else {
$self->error('Delete group failed.');
#my $error = LedgerSMB::Error->new("Delete group failed.");
#$error->set_status($status);
#return $error;
}
}
sub get_salutations {
my $self = shift;
my $sth = $self->{dbh}->prepare("SELECT * FROM salutation ORDER BY id ASC");
$sth->execute();
# Returns a list of hashrefs
return $sth->fetchall_arrayref( {} );
}
sub get_roles {
my $self = shift @_;
my @s_rows = $self->call_procedure(procname=>'admin__get_roles');
my @rows;
my $company = $self->{company};
for my $role (@s_rows) {
my $rolname = $role->{'rolname'};
$rolname =~ s/lsmb_${company}__//gi;
push @rows, $rolname;
}
return \@rows;
}
sub get_countries {
my $self = shift @_;
@{$self->{countries}}
=$self->exec_method(funcname => 'location_list_country');
# returns an array of hashrefs.
$self->debug({file => '/tmp/user'});
return $self->{countries};
}
sub get_contact_classes {
my $self = shift @_;
my $sth = $self->{dbh}->prepare("select id, class as name from contact_class");
my $code = $sth->execute();
return $sth->fetchall_arrayref({});
}
1;