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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
# --
# Kernel/System/CustomerUser/DB.pm - some customer user functions
# Copyright (C) 2001-2004 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: DB.pm,v 1.36 2004/12/28 01:03:00 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
package Kernel::System::CustomerUser::DB;
use strict;
use Kernel::System::CheckItem;
use vars qw(@ISA $VERSION);
$VERSION = '$Revision: 1.36 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;
# --
sub new {
my $Type = shift;
my %Param = @_;
# allocate new hash for object
my $Self = {};
bless ($Self, $Type);
# check needed objects
foreach (qw(DBObject ConfigObject LogObject PreferencesObject CustomerUserMap)) {
$Self->{$_} = $Param{$_} || die "Got no $_!";
}
# max shown user a search list
$Self->{UserSearchListLimit} = $Self->{CustomerUserMap}->{'CustomerUserSearchListLimit'} || 250;
# config options
$Self->{CustomerTable} = $Self->{CustomerUserMap}->{Params}->{Table}
|| die "Need CustomerUser->Params->Table in Kernel/Config.pm!";
$Self->{CustomerKey} = $Self->{CustomerUserMap}->{CustomerKey}
|| $Self->{CustomerUserMap}->{Key}
|| die "Need CustomerUser->CustomerKey in Kernel/Config.pm!";
$Self->{CustomerID} = $Self->{CustomerUserMap}->{CustomerID}
|| die "Need CustomerUser->CustomerID in Kernel/Config.pm!";
$Self->{ReadOnly} = $Self->{CustomerUserMap}->{ReadOnly};
$Self->{ExcludePrimaryCustomerID} = $Self->{CustomerUserMap}->{CustomerUserExcludePrimaryCustomerID} || 0;
$Self->{SearchPrefix} = $Self->{CustomerUserMap}->{'CustomerUserSearchPrefix'};
if (!defined($Self->{SearchPrefix})) {
$Self->{SearchPrefix} = '';
}
$Self->{SearchSuffix} = $Self->{CustomerUserMap}->{'CustomerUserSearchSuffix'};
if (!defined($Self->{SearchSuffix})) {
$Self->{SearchSuffix} = '*';
}
# create new db connect if DSN is given
if ($Self->{CustomerUserMap}->{Params}->{DSN}) {
$Self->{DBObject} = Kernel::System::DB->new(
LogObject => $Param{LogObject},
ConfigObject => $Param{ConfigObject},
DatabaseDSN => $Self->{CustomerUserMap}->{Params}->{DSN},
DatabaseUser => $Self->{CustomerUserMap}->{Params}->{User},
DatabasePw => $Self->{CustomerUserMap}->{Params}->{Password},
) || die $DBI::errstr;
# remember that we have the DBObject not from parent call
$Self->{NotParentDBObject} = 1;
}
# create check item object
$Self->{CheckItemObject} = Kernel::System::CheckItem->new(%Param);
return $Self;
}
# --
sub CustomerName {
my $Self = shift;
my %Param = @_;
my $Name = '';
# check needed stuff
if (!$Param{UserLogin}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need UserLogin!");
return;
}
# build SQL string 1/2
my $SQL = "SELECT $Self->{CustomerKey} ";
if ($Self->{CustomerUserMap}->{CustomerUserNameFields}) {
foreach my $Entry (@{$Self->{CustomerUserMap}->{CustomerUserNameFields}}) {
$SQL .= ", $Entry";
}
}
else {
$SQL .= " , first_name, last_name ";
}
$SQL .= " FROM $Self->{CustomerTable} WHERE ".
" $Self->{CustomerKey} = '".$Self->{DBObject}->Quote($Param{UserLogin})."'";
# get data
$Self->{DBObject}->Prepare(SQL => $SQL, Limit => 1);
while (my @Row = $Self->{DBObject}->FetchrowArray()) {
foreach (1..8) {
if ($Row[$_]) {
if (!$Name) {
$Name = $Row[$_];
}
else {
$Name .= ' '.$Row[$_];
}
}
}
}
return $Name;
}
# --
sub CustomerSearch {
my $Self = shift;
my %Param = @_;
my %Users = ();
my $Valid = defined $Param{Valid} ? $Param{Valid} : 1;
# check needed stuff
if (!$Param{Search} && !$Param{UserLogin} && !$Param{PostMasterSearch}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need Search, UserLogin or PostMasterSearch!");
return;
}
# build SQL string 1/2
my $SQL = "SELECT $Self->{CustomerKey} ";
if ($Self->{CustomerUserMap}->{CustomerUserListFields}) {
foreach my $Entry (@{$Self->{CustomerUserMap}->{CustomerUserListFields}}) {
$SQL .= ", $Entry";
}
}
else {
$SQL .= " , first_name, last_name, email ";
}
# build SQL string 2/2
$SQL .= " FROM " .
" $Self->{CustomerTable} ".
" WHERE ";
if ($Param{Search}) {
my $Count = 0;
my @Parts = split(/\+/, $Param{Search}, 6);
foreach my $Part (@Parts) {
$Part = $Self->{SearchPrefix}.$Part.$Self->{SearchSuffix};
$Part =~ s/\*/%/g;
$Part =~ s/%%/%/g;
if ($Count) {
$SQL .= " AND ";
}
$Count ++;
if ($Self->{CustomerUserMap}->{CustomerUserSearchFields}) {
my $SQLExt = '';
foreach (@{$Self->{CustomerUserMap}->{CustomerUserSearchFields}}) {
if ($SQLExt) {
$SQLExt .= ' OR ';
}
$SQLExt .= " LOWER($_) LIKE LOWER('".$Self->{DBObject}->Quote($Part)."') ";
}
if ($SQLExt) {
$SQL .= "($SQLExt)";
}
}
else {
$SQL .= " LOWER($Self->{CustomerKey}) LIKE LOWER('".$Self->{DBObject}->Quote($Part)."') ";
}
}
}
elsif ($Param{PostMasterSearch}) {
if ($Self->{CustomerUserMap}->{CustomerUserPostMasterSearchFields}) {
my $SQLExt = '';
foreach (@{$Self->{CustomerUserMap}->{CustomerUserPostMasterSearchFields}}) {
if ($SQLExt) {
$SQLExt .= ' OR ';
}
$SQLExt .= " LOWER($_) LIKE LOWER('".$Self->{DBObject}->Quote($Param{PostMasterSearch})."') ";
}
$SQL .= $SQLExt;
}
}
elsif ($Param{UserLogin}) {
$Param{UserLogin} =~ s/\*/%/g;
$SQL .= " LOWER($Self->{CustomerKey}) LIKE LOWER('".$Self->{DBObject}->Quote($Param{UserLogin})."')";
}
# add valid option
if ($Self->{CustomerUserMap}->{CustomerValid} && $Valid) {
$SQL .= "AND ".$Self->{CustomerUserMap}->{CustomerValid}.
" in ( ${\(join ', ', $Self->{DBObject}->GetValidIDs())} ) ";
}
# get data
$Self->{DBObject}->Prepare(SQL => $SQL, Limit => $Self->{UserSearchListLimit});
while (my @Row = $Self->{DBObject}->FetchrowArray()) {
foreach (1..8) {
if ($Row[$_]) {
$Users{$Row[0]} .= $Row[$_].' ';
}
}
$Users{$Row[0]} =~ s/^(.*)\s(.+?\@.+?\..+?)(\s|)$/"$1" <$2>/;
}
return %Users;
}
# --
sub CustomerUserList {
my $Self = shift;
my %Param = @_;
my $Valid = defined $Param{Valid} ? $Param{Valid} : 1;
# get data
my %Users = $Self->{DBObject}->GetTableData(
What => "$Self->{CustomerKey}, $Self->{CustomerKey}, $Self->{CustomerID}",
Table => $Self->{CustomerTable},
Clamp => 1,
Valid => $Valid,
);
return %Users;
}
# --
sub CustomerIDs {
my $Self = shift;
my %Param = @_;
my @CustomerIDs = ();
# check needed stuff
if (!$Param{User}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need User!");
return;
}
# get customer data
my %Data = $Self->CustomerUserDataGet(
User => $Param{User},
);
# there are multi customer ids
if ($Data{UserCustomerIDs}) {
foreach my $Split (';', ',', '|') {
if ($Data{UserCustomerIDs} =~ /$Split/) {
my @IDs = split(/$Split/, $Data{UserCustomerIDs});
foreach my $ID (@IDs) {
$ID =~ s/^\s+//g;
$ID =~ s/\s+$//g;
push (@CustomerIDs, $ID);
}
}
else {
$Data{UserCustomerIDs} =~ s/^\s+//g;
$Data{UserCustomerIDs} =~ s/\s+$//g;
push (@CustomerIDs, $Data{UserCustomerIDs});
}
}
}
# use also the primary customer id
if ($Data{UserCustomerID} && !$Self->{ExcludePrimaryCustomerID}) {
push (@CustomerIDs, $Data{UserCustomerID});
}
return @CustomerIDs;
}
# --
sub CustomerUserDataGet {
my $Self = shift;
my %Param = @_;
my %Data;
# check needed stuff
if (!$Param{User} && !$Param{CustomerID}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need User or CustomerID!");
return;
}
# build select
my $SQL = "SELECT ";
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
$SQL .= " $Entry->[2], ";
}
$SQL .= $Self->{CustomerKey}." FROM $Self->{CustomerTable} WHERE ";
if ($Param{User}) {
$SQL .= $Self->{CustomerKey}." = '".$Self->{DBObject}->Quote($Param{User})."'";
}
elsif ($Param{CustomerID}) {
$SQL .= $Self->{CustomerID}." = '".$Self->{DBObject}->Quote($Param{CustomerID})."'";
}
# get inital data
$Self->{DBObject}->Prepare(SQL => $SQL);
while (my @Row = $Self->{DBObject}->FetchrowArray()) {
my $MapCounter = 0;
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
$Data{$Entry->[0]} = $Row[$MapCounter];
$MapCounter++;
}
}
# check data
if (! exists $Data{UserLogin} && $Param{User}) {
# $Self->{LogObject}->Log(
# Priority => 'notice',
# Message => "Panic! No UserData for customer user: '$Param{User}'!!!",
# );
return;
}
if (! exists $Data{UserLogin} && $Param{CustomerID}) {
# $Self->{LogObject}->Log(
# Priority => 'notice',
# Message => "Panic! No UserData for customer id: '$Param{CustomerID}'!!!",
# );
return;
}
# compat!
$Data{UserID} = $Data{UserLogin};
# get preferences
my %Preferences = $Self->{PreferencesObject}->GetPreferences(UserID => $Data{UserID});
# return data
return (%Data, %Preferences);
}
# --
sub CustomerUserAdd {
my $Self = shift;
my %Param = @_;
# check ro/rw
if ($Self->{ReadOnly}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Customer backend is ro!");
return;
}
# check needed stuff
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
if (!$Param{$Entry->[0]} && $Entry->[4]) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need $Entry->[0]!");
return;
}
}
if (!$Param{UserID}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need UserID!");
return;
}
# just if no UserLogin is given
if (!$Param{UserLogin} && $Self->{CustomerUserMap}->{AutoLoginCreation}) {
my ($Sec, $Min, $Hour, $Day, $Month, $Year, $WDay) = localtime(time());
$Year = $Year-100;
$Year = "0$Year" if ($Year <10);
$Month = $Month+1;
$Month = "0$Month" if ($Month <10);
$Day = "0$Day" if ($Day <10);
$Hour = "0$Hour" if ($Hour <10);
$Min = "0$Min" if ($Min <10);
my $Prefix = $Self->{CustomerUserMap}->{AutoLoginCreationPrefix} || 'auto';
$Param{UserLogin} = "$Prefix-$Year$Month$Day$Hour$Min".int(rand(99));
}
# check if user login exists
if (!$Param{UserLogin}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need UserLogin!");
return;
}
# check email address if already exists
if ($Param{UserEmail} && $Self->{CustomerUserMap}->{CustomerUserEmailUniqCheck}) {
my %Result = $Self->CustomerSearch(
Valid => 1,
PostMasterSearch => $Param{UserEmail},
);
if (%Result) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Email already exists!");
return;
}
}
# check email address mx
if ($Param{UserEmail} && !$Self->{CheckItemObject}->CheckEmail(Address => $Param{UserEmail})) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Email address ($Param{UserEmail}) not valid (".
$Self->{CheckItemObject}->CheckError().")!",
);
return;
}
# build insert
my $SQL = "INSERT INTO $Self->{CustomerTable} (";
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
$SQL .= " $Entry->[2], ";
}
$SQL .= "create_time, create_by, change_time, change_by)";
$SQL .= " VALUES (";
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
if ($Entry->[5] =~ /^int$/i) {
$SQL .= " ".$Self->{DBObject}->Quote($Param{$Entry->[0]}).", ";
}
else {
$SQL .= " '".$Self->{DBObject}->Quote($Param{$Entry->[0]})."', ";
}
}
$SQL .= "current_timestamp, $Param{UserID}, current_timestamp, $Param{UserID})";
if ($Self->{DBObject}->Do(SQL => $SQL)) {
# log notice
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "CustomerUser: '$Param{UserLogin}' created successfully ($Param{UserID})!",
);
# set password
if ($Param{UserPassword}) {
$Self->SetPassword(UserLogin => $Param{UserLogin}, PW => $Param{UserPassword});
}
return $Param{UserLogin};
}
else {
return;
}
}
# --
sub CustomerUserUpdate {
my $Self = shift;
my %Param = @_;
# check ro/rw
if ($Self->{ReadOnly}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Customer backend is ro!");
return;
}
# check needed stuff
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
if (!$Param{$Entry->[0]} && $Entry->[4] && $Entry->[0] ne 'UserPassword') {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need $Entry->[0]!");
return;
}
}
# check email address
if ($Param{UserEmail} && !$Self->{CheckItemObject}->CheckEmail(Address => $Param{UserEmail})) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Email address ($Param{UserEmail}) not valid (".
$Self->{CheckItemObject}->CheckError().")!",
);
return;
}
# get old user data (pw)
my %UserData = $Self->CustomerUserDataGet(User => $Param{ID});
# update db
my $SQL = "UPDATE $Self->{CustomerTable} SET ";
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
if ($Entry->[5] =~ /^int$/i) {
$SQL .= " $Entry->[2] = ".$Self->{DBObject}->Quote($Param{$Entry->[0]}).", ";
}
elsif ($Entry->[0] !~ /^UserPassword$/i) {
$SQL .= " $Entry->[2] = '".$Self->{DBObject}->Quote($Param{$Entry->[0]})."', ";
}
}
$SQL .= " change_time = current_timestamp, ";
$SQL .= " change_by = $Param{UserID} ";
$SQL .= " WHERE ".$Self->{CustomerKey}." = '".$Self->{DBObject}->Quote($Param{ID})."'";
if ($Self->{DBObject}->Do(SQL => $SQL)) {
# log notice
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "CustomerUser: '$Param{UserLogin}' updated successfully ($Param{UserID})!",
);
# check pw
if ($Param{UserPassword}) {
$Self->SetPassword(UserLogin => $Param{UserLogin}, PW => $Param{UserPassword});
}
return 1;
}
else {
return;
}
}
# --
sub SetPassword {
my $Self = shift;
my %Param = @_;
my $Pw = $Param{PW} || '';
# check ro/rw
if ($Self->{ReadOnly}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Customer backend is ro!");
return;
}
# check needed stuff
if (!$Param{UserLogin}) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Need UserLogin!");
return;
}
# crypt given pw (unfortunately there is a mod_perl2 bug on RH8 - check if
# crypt() is working correctly) :-/
my $CryptedPw = '';
if (crypt('root', 'root@localhost') eq 'roK20XGbWEsSM') {
$CryptedPw = crypt($Pw, $Param{UserLogin});
}
else {
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "The crypt() of your mod_perl(2) is not working correctly! Update mod_perl!",
);
my $TempUser = quotemeta($Param{UserLogin});
my $TempPw = quotemeta($Pw);
my $CMD = "perl -e \"print crypt('$TempPw', '$TempUser');\"";
open (IO, " $CMD | ") || print STDERR "Can't open $CMD: $!";
while (<IO>) {
$CryptedPw .= $_;
}
close (IO);
chomp $CryptedPw;
}
# update db
foreach my $Entry (@{$Self->{CustomerUserMap}->{Map}}) {
if ($Entry->[0] =~ /^UserPassword$/i) {
$Param{PasswordCol} = $Entry->[2];
}
if ($Entry->[0] =~ /^UserLogin$/i) {
$Param{LoginCol} = $Entry->[2];
}
}
# check if needed pw col. exists (else there is no pw col.)
if ($Param{PasswordCol} && $Param{LoginCol}) {
if ($Self->{DBObject}->Do(
SQL => "UPDATE $Self->{CustomerTable} ".
" SET ".
" $Param{PasswordCol} = '".$Self->{DBObject}->Quote($CryptedPw)."' ".
" WHERE ".
" $Param{LoginCol} = '".$Self->{DBObject}->Quote($Param{UserLogin})."'",
)) {
# log notice
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "CustomerUser: '$Param{UserLogin}' changed password successfully!",
);
return 1;
}
else {
return;
}
}
else {
# need no pw to set
return 1;
}
}
# --
sub GenerateRandomPassword {
my $Self = shift;
my %Param = @_;
# Generated passwords are eight characters long by default.
my $Size = $Param{Size} || 8;
# The list of characters that can appear in a randomly generated password.
# Note that users can put any character into a password they choose themselves.
my @PwChars = (0..9, 'A'..'Z', 'a'..'z', '-', '_', '!', '@', '#', '$', '%', '^', '&', '*');
# The number of characters in the list.
my $PwCharsLen = scalar(@PwChars);
# Generate the password.
my $Password = '';
for ( my $i=0 ; $i<$Size ; $i++ ) {
$Password .= $PwChars[rand($PwCharsLen)];
}
# Return the password.
return $Password;
}
# --
sub DESTROY {
my $Self = shift;
# disconnect if it's not a parent DBObject
if ($Self->{NotParentDBObject}) {
if ($Self->{DBObject}) {
$Self->{DBObject}->Disconnect();
}
}
}
# --
1;
|