/
InterfaceAgent.pm
1268 lines (1084 loc) · 43.4 KB
/
InterfaceAgent.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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# 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 https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Web::InterfaceAgent;
use strict;
use warnings;
use Kernel::Language qw(Translatable);
use Kernel::System::DateTime;
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::Output::HTML::Layout',
'Kernel::System::Auth',
'Kernel::System::AuthSession',
'Kernel::System::DB',
'Kernel::System::Email',
'Kernel::System::Group',
'Kernel::System::Log',
'Kernel::System::Main',
'Kernel::System::Scheduler',
'Kernel::System::DateTime',
'Kernel::System::User',
'Kernel::System::Web::Request',
'Kernel::System::Valid',
);
=head1 NAME
Kernel::System::Web::InterfaceAgent - the agent web interface
=head1 DESCRIPTION
the global agent web interface (authentication, session handling, ...)
=head1 PUBLIC INTERFACE
=head2 new()
create agent web interface object. Do not use it directly, instead use:
use Kernel::System::ObjectManager;
my $Debug = 0,
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Web::InterfaceAgent' => {
Debug => 0,
WebRequest => CGI::Fast->new(), # optional, e. g. if fast cgi is used,
# the CGI object is already provided
}
);
my $InterfaceAgent = $Kernel::OM->Get('Kernel::System::Web::InterfaceAgent');
=cut
sub new {
my ( $Type, %Param ) = @_;
my $Self = {};
bless( $Self, $Type );
# Performance log
$Self->{PerformanceLogStart} = time();
# get debug level
$Self->{Debug} = $Param{Debug} || 0;
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Log' => {
LogPrefix => $Kernel::OM->Get('Kernel::Config')->Get('CGILogPrefix'),
},
'Kernel::System::Web::Request' => {
WebRequest => $Param{WebRequest} || 0,
},
);
# debug info
if ( $Self->{Debug} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'debug',
Message => 'Global handle started...',
);
}
return $Self;
}
=head2 Run()
execute the object
$InterfaceAgent->Run();
=cut
sub Run {
my $Self = shift;
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $QueryString = $ENV{QUERY_STRING} || '';
# Check if https forcing is active, and redirect if needed.
if ( $ConfigObject->Get('HTTPSForceRedirect') ) {
# Some web servers do not set HTTPS environment variable, so it's not possible to easily know if we are using
# https protocol. Look also for similarly named keys in environment hash, since this should prevent loops in
# certain cases.
if (
(
!defined $ENV{HTTPS}
&& !grep {/^HTTPS(?:_|$)/} keys %ENV
)
|| $ENV{HTTPS} ne 'on'
)
{
my $Host = $ENV{HTTP_HOST} || $ConfigObject->Get('FQDN');
# Redirect with 301 code. Add two new lines at the end, so HTTP headers are validated correctly.
print "Status: 301 Moved Permanently\nLocation: https://$Host$ENV{REQUEST_URI}\n\n";
return;
}
}
my $ParamObject = $Kernel::OM->Get('Kernel::System::Web::Request');
my %Param;
# get session id
$Param{SessionName} = $ConfigObject->Get('SessionName') || 'SessionID';
$Param{SessionID} = $ParamObject->GetParam( Param => $Param{SessionName} ) || '';
# drop old session id (if exists)
$QueryString =~ s/(\?|&|;|)$Param{SessionName}(=&|=;|=.+?&|=.+?$)/;/g;
# define framework params
my $FrameworkParams = {
Lang => '',
Action => '',
Subaction => '',
RequestedURL => $QueryString,
};
for my $Key ( sort keys %{$FrameworkParams} ) {
$Param{$Key} = $ParamObject->GetParam( Param => $Key )
|| $FrameworkParams->{$Key};
}
# validate language
if ( $Param{Lang} && $Param{Lang} !~ m{\A[a-z]{2}(?:_[A-Z]{2})?\z}xms ) {
delete $Param{Lang};
}
# check if the browser sends the SessionID cookie and set the SessionID-cookie
# as SessionID! GET or POST SessionID have the lowest priority.
my $BrowserHasCookie = 0;
if ( $ConfigObject->Get('SessionUseCookie') ) {
$Param{SessionIDCookie} = $ParamObject->GetCookie( Key => $Param{SessionName} );
if ( $Param{SessionIDCookie} ) {
$Param{SessionID} = $Param{SessionIDCookie};
}
}
$Kernel::OM->ObjectParamAdd(
'Kernel::Output::HTML::Layout' => {
Lang => $Param{Lang},
UserLanguage => $Param{Lang},
},
'Kernel::Language' => {
UserLanguage => $Param{Lang}
},
);
my $CookieSecureAttribute;
if ( $ConfigObject->Get('HttpType') eq 'https' ) {
# Restrict Cookie to HTTPS if it is used.
$CookieSecureAttribute = 1;
}
my $DBCanConnect = $Kernel::OM->Get('Kernel::System::DB')->Connect();
if ( !$DBCanConnect || $ParamObject->Error() ) {
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
if ( !$DBCanConnect ) {
$LayoutObject->FatalError(
Comment => Translatable('Please contact the administrator.'),
);
return;
}
if ( $ParamObject->Error() ) {
$LayoutObject->FatalError(
Message => $ParamObject->Error(),
Comment => Translatable('Please contact the administrator.'),
);
return;
}
}
# get common application and add-on application params
my %CommonObjectParam = %{ $ConfigObject->Get('Frontend::CommonParam') };
for my $Key ( sort keys %CommonObjectParam ) {
$Param{$Key} = $ParamObject->GetParam( Param => $Key ) || $CommonObjectParam{$Key};
}
# security check Action Param (replace non word chars)
$Param{Action} =~ s/\W//g;
my $SessionObject = $Kernel::OM->Get('Kernel::System::AuthSession');
my $UserObject = $Kernel::OM->Get('Kernel::System::User');
# check request type
if ( $Param{Action} eq 'PreLogin' ) {
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
$Param{RequestedURL} = $Param{RequestedURL} || "Action=AgentDashboard";
# login screen
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Mode => 'PreLogin',
%Param,
),
);
return;
}
elsif ( $Param{Action} eq 'Login' ) {
# get params
my $PostUser = $ParamObject->GetParam( Param => 'User' ) || '';
my $PostPw = $ParamObject->GetParam(
Param => 'Password',
Raw => 1
) || '';
my $PostTwoFactorToken = $ParamObject->GetParam(
Param => 'TwoFactorToken',
Raw => 1
) || '';
# create AuthObject
my $AuthObject = $Kernel::OM->Get('Kernel::System::Auth');
# check submitted data
my $User = $AuthObject->Auth(
User => $PostUser,
Pw => $PostPw,
TwoFactorToken => $PostTwoFactorToken,
);
# login is invalid
if ( !$User ) {
my $Expires = '+' . $ConfigObject->Get('SessionMaxTime') . 's';
if ( !$ConfigObject->Get('SessionUseCookieAfterBrowserClose') ) {
$Expires = '';
}
$Kernel::OM->ObjectParamAdd(
'Kernel::Output::HTML::Layout' => {
SetCookies => {
OTRSBrowserHasCookie => $ParamObject->SetCookie(
Key => 'OTRSBrowserHasCookie',
Value => 1,
Expires => $Expires,
Path => $ConfigObject->Get('ScriptAlias'),
Secure => $CookieSecureAttribute,
HTTPOnly => 1,
),
},
}
);
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# redirect to alternate login
if ( $ConfigObject->Get('LoginURL') ) {
$Param{RequestedURL} = $LayoutObject->LinkEncode( $Param{RequestedURL} );
print $LayoutObject->Redirect(
ExtURL => $ConfigObject->Get('LoginURL')
. "?Reason=LoginFailed&RequestedURL=$Param{RequestedURL}",
);
return;
}
# show normal login
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => $Kernel::OM->Get('Kernel::System::Log')->GetLogEntry(
Type => 'Info',
What => 'Message',
)
|| $LayoutObject->{LanguageObject}->Translate( $AuthObject->GetLastErrorMessage() )
|| Translatable('Login failed! Your user name or password was entered incorrectly.'),
LoginFailed => 1,
MessageType => 'Error',
User => $User,
%Param,
),
);
return;
}
# login is successful
my %UserData = $UserObject->GetUserData(
User => $User,
Valid => 1,
NoOutOfOffice => 1,
);
# check if the browser supports cookies
if ( $ParamObject->GetCookie( Key => 'OTRSBrowserHasCookie' ) ) {
$Kernel::OM->ObjectParamAdd(
'Kernel::Output::HTML::Layout' => {
BrowserHasCookie => 1,
},
);
}
# check needed data
if ( !$UserData{UserID} || !$UserData{UserLogin} ) {
# redirect to alternate login
if ( $ConfigObject->Get('LoginURL') ) {
print $Kernel::OM->Get('Kernel::Output::HTML::Layout')->Redirect(
ExtURL => $ConfigObject->Get('LoginURL') . '?Reason=SystemError',
);
return;
}
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# show need user data error message
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Error',
Message =>
Translatable(
'Authentication succeeded, but no user data record is found in the database. Please contact the administrator.'
),
%Param,
MessageType => 'Error',
),
);
return;
}
my $DateTimeObj = $Kernel::OM->Create('Kernel::System::DateTime');
# create new session id
my $NewSessionID = $SessionObject->CreateSessionID(
%UserData,
UserLastRequest => $DateTimeObj->ToEpoch(),
UserType => 'User',
SessionSource => 'AgentInterface',
);
# show error message if no session id has been created
if ( !$NewSessionID ) {
# get error message
my $Error = $SessionObject->SessionIDErrorMessage() || '';
# output error message
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => $Error,
MessageType => 'Error',
%Param,
),
);
return;
}
# execution in 20 seconds
my $ExecutionTimeObj = $DateTimeObj->Clone();
$ExecutionTimeObj->Add( Seconds => 20 );
my $ExecutionTime = $ExecutionTimeObj->ToString();
# add a asychronous executor scheduler task to count the concurrent user
$Kernel::OM->Get('Kernel::System::Scheduler')->TaskAdd(
ExecutionTime => $ExecutionTime,
Type => 'AsynchronousExecutor',
Name => 'PluginAsynchronous::ConcurrentUser',
MaximumParallelInstances => 1,
Data => {
Object => 'Kernel::System::SupportDataCollector::PluginAsynchronous::OTRS::ConcurrentUsers',
Function => 'RunAsynchronous',
},
);
my $UserTimeZone = $Self->_UserTimeZoneGet(%UserData);
$SessionObject->UpdateSessionID(
SessionID => $NewSessionID,
Key => 'UserTimeZone',
Value => $UserTimeZone,
);
# check if the time zone offset reported by the user's browser differs from that
# of the OTRS user's time zone offset
my $DateTimeObject = $Kernel::OM->Create(
'Kernel::System::DateTime',
ObjectParams => {
TimeZone => $UserTimeZone,
},
);
my $OTRSUserTimeZoneOffset = $DateTimeObject->Format( Format => '%{offset}' ) / 60;
my $BrowserTimeZoneOffset = ( $ParamObject->GetParam( Param => 'TimeZoneOffset' ) || 0 ) * -1;
# TimeZoneOffsetDifference contains the difference of the time zone offset between
# the user's OTRS time zone setting and the one reported by the user's browser.
# If there is a difference it can be evaluated later to e. g. show a message
# for the user to check his OTRS time zone setting.
my $UserTimeZoneOffsetDifference = abs( $OTRSUserTimeZoneOffset - $BrowserTimeZoneOffset );
$SessionObject->UpdateSessionID(
SessionID => $NewSessionID,
Key => 'UserTimeZoneOffsetDifference',
Value => $UserTimeZoneOffsetDifference,
);
# create a new LayoutObject with SessionIDCookie
my $Expires = '+' . $ConfigObject->Get('SessionMaxTime') . 's';
if ( !$ConfigObject->Get('SessionUseCookieAfterBrowserClose') ) {
$Expires = '';
}
my $SecureAttribute;
if ( $ConfigObject->Get('HttpType') eq 'https' ) {
# Restrict Cookie to HTTPS if it is used.
$SecureAttribute = 1;
}
$Kernel::OM->ObjectParamAdd(
'Kernel::Output::HTML::Layout' => {
SetCookies => {
SessionIDCookie => $ParamObject->SetCookie(
Key => $Param{SessionName},
Value => $NewSessionID,
Expires => $Expires,
Path => $ConfigObject->Get('ScriptAlias'),
Secure => scalar $CookieSecureAttribute,
HTTPOnly => 1,
),
OTRSBrowserHasCookie => $ParamObject->SetCookie(
Key => 'OTRSBrowserHasCookie',
Value => '',
Expires => '-1y',
Path => $ConfigObject->Get('ScriptAlias'),
Secure => $CookieSecureAttribute,
HTTPOnly => 1,
),
},
SessionID => $NewSessionID,
SessionName => $Param{SessionName},
},
);
# Check if Chat is active
if ( $Kernel::OM->Get('Kernel::Config')->Get('ChatEngine::Active') ) {
my $ChatReceivingAgentsGroup
= $Kernel::OM->Get('Kernel::Config')->Get('ChatEngine::PermissionGroup::ChatReceivingAgents');
my $ChatReceivingAgentsGroupPermission = $Kernel::OM->Get('Kernel::System::Group')->PermissionCheck(
UserID => $UserData{UserID},
GroupName => $ChatReceivingAgentsGroup,
Type => 'rw',
);
if (
$UserData{UserID} != -1
&& $ChatReceivingAgentsGroup
&& $ChatReceivingAgentsGroupPermission
&& $Kernel::OM->Get('Kernel::Config')->Get('Ticket::Agent::UnavailableForExternalChatsOnLogin')
)
{
# Get user preferences
my %Preferences = $Kernel::OM->Get('Kernel::System::User')->GetPreferences(
UserID => $UserData{UserID},
);
if ( $Preferences{ChatAvailability} && $Preferences{ChatAvailability} == 2 ) {
# User is available for external chats. Set his availability to internal only.
$Kernel::OM->Get('Kernel::System::User')->SetPreferences(
Key => 'ChatAvailability',
Value => '1',
UserID => $UserData{UserID},
);
# Set ChatAvailabilityNotification to display notification in agent interface (only once)
$Kernel::OM->Get('Kernel::System::User')->SetPreferences(
Key => 'ChatAvailabilityNotification',
Value => '1',
UserID => $UserData{UserID},
);
}
}
}
# redirect with new session id and old params
# prepare old redirect URL -- do not redirect to Login or Logout (loop)!
if ( $Param{RequestedURL} =~ /Action=(Logout|Login|LostPassword|PreLogin)/ ) {
$Param{RequestedURL} = '';
}
# redirect with new session id
print $Kernel::OM->Get('Kernel::Output::HTML::Layout')->Redirect(
OP => $Param{RequestedURL},
Login => 1,
);
return 1;
}
# logout
elsif ( $Param{Action} eq 'Logout' ) {
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# check session id
if ( !$SessionObject->CheckSessionID( SessionID => $Param{SessionID} ) ) {
# redirect to alternate login
if ( $ConfigObject->Get('LoginURL') ) {
$Param{RequestedURL} = $LayoutObject->LinkEncode( $Param{RequestedURL} );
print $LayoutObject->Redirect(
ExtURL => $ConfigObject->Get('LoginURL')
. "?Reason=InvalidSessionID&RequestedURL=$Param{RequestedURL}",
);
return;
}
# show login screen
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Logout',
%Param,
),
);
return;
}
# get session data
my %UserData = $SessionObject->GetSessionIDData(
SessionID => $Param{SessionID},
);
$UserData{UserTimeZone} = $Self->_UserTimeZoneGet(%UserData);
# create a new LayoutObject with %UserData
$Kernel::OM->ObjectParamAdd(
'Kernel::Output::HTML::Layout' => {
SetCookies => {
SessionIDCookie => $ParamObject->SetCookie(
Key => $Param{SessionName},
Value => '',
Expires => '-1y',
Path => $ConfigObject->Get('ScriptAlias'),
Secure => scalar $CookieSecureAttribute,
HTTPOnly => 1,
),
},
%UserData,
},
);
$Kernel::OM->ObjectsDiscard( Objects => ['Kernel::Output::HTML::Layout'] );
$LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# Prevent CSRF attacks
$LayoutObject->ChallengeTokenCheck();
# remove session id
if ( !$SessionObject->RemoveSessionID( SessionID => $Param{SessionID} ) ) {
$LayoutObject->FatalError(
Message => Translatable('Can`t remove SessionID.'),
Comment => Translatable('Please contact the administrator.'),
);
return;
}
# redirect to alternate login
if ( $ConfigObject->Get('LogoutURL') ) {
print $LayoutObject->Redirect(
ExtURL => $ConfigObject->Get('LogoutURL'),
);
return 1;
}
# show logout screen
my $LogoutMessage = $LayoutObject->{LanguageObject}->Translate('Logout successful.');
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Logout',
Message => $LogoutMessage,
MessageType => 'Success',
%Param,
),
);
return 1;
}
# user lost password
elsif ( $Param{Action} eq 'LostPassword' ) {
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# check feature
if ( !$ConfigObject->Get('LostPassword') ) {
# show normal login
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => Translatable('Feature not active!'),
MessageType => 'Error',
),
);
return;
}
# get params
my $User = $ParamObject->GetParam( Param => 'User' ) || '';
my $Token = $ParamObject->GetParam( Param => 'Token' ) || '';
# get user login by token
if ( !$User && $Token ) {
# Prevent extracting password reset token character-by-character via wildcard injection
# The wild card characters "%" and "_" could be used to match arbitrary character.
if ( $Token !~ m{\A (?: [a-zA-Z] | \d )+ \z}xms ) {
# Security: pretend that password reset instructions were actually sent to
# make sure that users cannot find out valid usernames by
# just trying and checking the result message.
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => Translatable('Sent password reset instructions. Please check your email.'),
MessageType => 'Success',
%Param,
),
);
return;
}
my %UserList = $UserObject->SearchPreferences(
Key => 'UserToken',
Value => $Token,
);
USERS:
for my $UserID ( sort keys %UserList ) {
my %UserData = $UserObject->GetUserData(
UserID => $UserID,
Valid => 1,
);
if (%UserData) {
$User = $UserData{UserLogin};
last USERS;
}
}
}
# get user data
my %UserData = $UserObject->GetUserData(
User => $User,
Valid => 1
);
# verify user is valid when requesting password reset
my @ValidIDs = $Kernel::OM->Get('Kernel::System::Valid')->ValidIDsGet();
my $UserIsValid = grep { $UserData{ValidID} && $UserData{ValidID} == $_ } @ValidIDs;
if ( !$UserData{UserID} || !$UserIsValid ) {
# Security: pretend that password reset instructions were actually sent to
# make sure that users cannot find out valid usernames by
# just trying and checking the result message.
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => Translatable('Sent password reset instructions. Please check your email.'),
MessageType => 'Success',
%Param,
),
);
return;
}
# create email object
my $EmailObject = $Kernel::OM->Get('Kernel::System::Email');
# send password reset token
if ( !$Token ) {
# generate token
$UserData{Token} = $UserObject->TokenGenerate(
UserID => $UserData{UserID},
);
# send token notify email with link
my $Body = $ConfigObject->Get('NotificationBodyLostPasswordToken')
|| 'ERROR: NotificationBodyLostPasswordToken is missing!';
my $Subject = $ConfigObject->Get('NotificationSubjectLostPasswordToken')
|| 'ERROR: NotificationSubjectLostPasswordToken is missing!';
for my $Key ( sort keys %UserData ) {
$Body =~ s/<OTRS_$Key>/$UserData{$Key}/gi;
}
my $Sent = $EmailObject->Send(
To => $UserData{UserEmail},
Subject => $Subject,
Charset => $LayoutObject->{UserCharset},
MimeType => 'text/plain',
Body => $Body
);
if ( !$Sent->{Success} ) {
$LayoutObject->FatalError(
Comment => Translatable('Please contact the administrator.'),
);
return;
}
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => Translatable('Sent password reset instructions. Please check your email.'),
MessageType => 'Success',
%Param,
),
);
return 1;
}
# reset password
# check if token is valid
my $TokenValid = $UserObject->TokenCheck(
Token => $Token,
UserID => $UserData{UserID},
);
if ( !$TokenValid ) {
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => Translatable('Invalid Token!'),
MessageType => 'Error',
%Param,
),
);
return;
}
# get new password
$UserData{NewPW} = $UserObject->GenerateRandomPassword();
# update new password
$UserObject->SetPassword(
UserLogin => $User,
PW => $UserData{NewPW}
);
# send notify email
my $Body = $ConfigObject->Get('NotificationBodyLostPassword')
|| 'New Password is: <OTRS_NEWPW>';
my $Subject = $ConfigObject->Get('NotificationSubjectLostPassword')
|| 'New Password!';
for my $Key ( sort keys %UserData ) {
$Body =~ s/<OTRS_$Key>/$UserData{$Key}/gi;
}
my $Sent = $EmailObject->Send(
To => $UserData{UserEmail},
Subject => $Subject,
Charset => $LayoutObject->{UserCharset},
MimeType => 'text/plain',
Body => $Body
);
if ( !$Sent->{Success} ) {
$LayoutObject->FatalError(
Comment => Translatable('Please contact the administrator.'),
);
return;
}
my $Message = $LayoutObject->{LanguageObject}->Translate(
'Sent new password to %s. Please check your email.',
$UserData{UserEmail},
);
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message => $Message,
User => $User,
MessageType => 'Success',
%Param,
),
);
return 1;
}
# show login site
elsif ( !$Param{SessionID} ) {
# create AuthObject
my $AuthObject = $Kernel::OM->Get('Kernel::System::Auth');
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
if ( $AuthObject->GetOption( What => 'PreAuth' ) ) {
# automatic login
$Param{RequestedURL} = $LayoutObject->LinkEncode( $Param{RequestedURL} );
print $LayoutObject->Redirect(
OP => "Action=PreLogin&RequestedURL=$Param{RequestedURL}",
);
return;
}
elsif ( $ConfigObject->Get('LoginURL') ) {
# redirect to alternate login
$Param{RequestedURL} = $LayoutObject->LinkEncode( $Param{RequestedURL} );
print $LayoutObject->Redirect(
ExtURL => $ConfigObject->Get('LoginURL')
. "?RequestedURL=$Param{RequestedURL}",
);
return;
}
# login screen
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
%Param,
),
);
return;
}
# run modules if a version value exists
elsif ( $Kernel::OM->Get('Kernel::System::Main')->Require("Kernel::Modules::$Param{Action}") ) {
# check session id
if ( !$SessionObject->CheckSessionID( SessionID => $Param{SessionID} ) ) {
# put '%Param' into LayoutObject
$Kernel::OM->ObjectParamAdd(
'Kernel::Output::HTML::Layout' => {
SetCookies => {
SessionIDCookie => $ParamObject->SetCookie(
Key => $Param{SessionName},
Value => '',
Expires => '-1y',
Path => $ConfigObject->Get('ScriptAlias'),
Secure => scalar $CookieSecureAttribute,
HTTPOnly => 1,
),
},
%Param,
},
);
$Kernel::OM->ObjectsDiscard( Objects => ['Kernel::Output::HTML::Layout'] );
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# create AuthObject
my $AuthObject = $Kernel::OM->Get('Kernel::System::Auth');
if ( $AuthObject->GetOption( What => 'PreAuth' ) ) {
# automatic re-login
$Param{RequestedURL} = $LayoutObject->LinkEncode( $Param{RequestedURL} );
print $LayoutObject->Redirect(
OP => "?Action=PreLogin&RequestedURL=$Param{RequestedURL}",
);
return;
}
elsif ( $ConfigObject->Get('LoginURL') ) {
# redirect to alternate login
$Param{RequestedURL} = $LayoutObject->LinkEncode( $Param{RequestedURL} );
print $LayoutObject->Redirect(
ExtURL => $ConfigObject->Get('LoginURL')
. "?Reason=InvalidSessionID&RequestedURL=$Param{RequestedURL}",
);
return;
}
# show login
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Login',
Message =>
$LayoutObject->{LanguageObject}->Translate( $SessionObject->SessionIDErrorMessage() ),
MessageType => 'Error',
%Param,
),
);
return;
}
# get session data
my %UserData = $SessionObject->GetSessionIDData(
SessionID => $Param{SessionID},
);
$UserData{UserTimeZone} = $Self->_UserTimeZoneGet(%UserData);
# check needed data
if ( !$UserData{UserID} || !$UserData{UserLogin} || $UserData{UserType} ne 'User' ) {
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
# redirect to alternate login
if ( $ConfigObject->Get('LoginURL') ) {
print $LayoutObject->Redirect(
ExtURL => $ConfigObject->Get('LoginURL') . '?Reason=SystemError',
);
return;
}
# show login screen
$LayoutObject->Print(
Output => \$LayoutObject->Login(
Title => 'Error',
Message => Translatable('Error: invalid session.'),
MessageType => 'Error',
%Param,
),
);
return;
}
# check module registry
my $ModuleReg = $ConfigObject->Get('Frontend::Module')->{ $Param{Action} };
if ( !$ModuleReg ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message =>
"Module Kernel::Modules::$Param{Action} not registered in Kernel/Config.pm!",
);
$Kernel::OM->Get('Kernel::Output::HTML::Layout')->FatalError(
Comment => Translatable('Please contact the administrator.'),
);
return;
}
# module permisson check
if (
ref $ModuleReg->{GroupRo} eq 'ARRAY'
&& !scalar @{ $ModuleReg->{GroupRo} }
&& ref $ModuleReg->{Group} eq 'ARRAY'
&& !scalar @{ $ModuleReg->{Group} }
)
{
$Param{AccessRo} = 1;
$Param{AccessRw} = 1;
}
else {
my $GroupObject = $Kernel::OM->Get('Kernel::System::Group');
PERMISSION:
for my $Permission (qw(GroupRo Group)) {
my $AccessOk = 0;
my $Group = $ModuleReg->{$Permission};
next PERMISSION if !$Group;
if ( ref $Group eq 'ARRAY' ) {
INNER:
for my $GroupName ( @{$Group} ) {
next INNER if !$GroupName;
next INNER if !$GroupObject->PermissionCheck(
UserID => $UserData{UserID},
GroupName => $GroupName,
Type => $Permission eq 'GroupRo' ? 'ro' : 'rw',
);
$AccessOk = 1;
last INNER;
}
}
else {
my $HasPermission = $GroupObject->PermissionCheck(
UserID => $UserData{UserID},
GroupName => $Group,
Type => $Permission eq 'GroupRo' ? 'ro' : 'rw',
);
if ($HasPermission) {
$AccessOk = 1;
}
}
if ( $Permission eq 'Group' && $AccessOk ) {
$Param{AccessRo} = 1;
$Param{AccessRw} = 1;
}
elsif ( $Permission eq 'GroupRo' && $AccessOk ) {
$Param{AccessRo} = 1;
}
}
if ( !$Param{AccessRo} && !$Param{AccessRw} || !$Param{AccessRo} && $Param{AccessRw} ) {