-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathznuny.SessionCreate.pl
More file actions
190 lines (158 loc) · 5.35 KB
/
znuny.SessionCreate.pl
File metadata and controls
190 lines (158 loc) · 5.35 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
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl
# --
# bin/znuny.SessionCreate.pl - prints an URL to the OTRS with a valid SessionID for the wanted User or CustomerUser
# Copyright (C) 2014 Znuny GmbH, http://znuny.com/
# --
use strict;
use warnings;
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';
use lib dirname($RealBin) . '/Custom';
use Getopt::Long;
use Kernel::Config;
use Kernel::System::Time;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::Encode;
use Kernel::System::DB;
use Kernel::System::User;
use Kernel::System::CustomerUser;
use Kernel::System::Group;
use Kernel::System::CustomerGroup;
use Kernel::System::AuthSession;
# get options
my %Opts;
GetOptions(
'userlogin|u=s' => \$Opts{UserLogin},
'usertype|t=s' => \$Opts{UserType},
'help|h' => \$Opts{Help},
);
# common objects
my %CommonObject;
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject} = Kernel::System::Log->new(
LogPrefix => 'OTRS-znuny.SessionCreate.pl',
%CommonObject,
);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);
$CommonObject{CustomerUserObject} = Kernel::System::CustomerUser->new(%CommonObject);
$CommonObject{GroupObject} = Kernel::System::Group->new(%CommonObject);
$CommonObject{CustomerGroupObject} = Kernel::System::CustomerGroup->new(%CommonObject);
$CommonObject{SessionObject} = Kernel::System::AuthSession->new(%CommonObject);
if (
$Opts{Help}
|| !$Opts{UserLogin}
) {
print STDOUT "znuny.SessionCreate.pl - prints an URL to the OTRS with a valid SessionID for the wanted User or CustomerUser\n";
print STDOUT "Copyright (C) 2014 Znuny GmbH, http://znuny.com/\n";
print STDOUT "usage: znuny.SessionCreate.pl
Required parameters:
-[-u]serlogin - the login of the user for which the session should be created
Optional parameters:
--user[t]ype - define the session type 'User' (default) or 'Customer'
-[-h]elp - print this help text\n";
exit 0;
}
# set default if no value given
$Opts{UserType} ||= 'User';
if ( lc $Opts{UserType} eq lc 'User' ) {
$Opts{UserType} = 'User';
}
elsif ( lc $Opts{UserType} eq lc 'Customer' ) {
$Opts{UserType} = 'Customer';
}
else {
$CommonObject{LogObject}->Log(
Priority => 'error',
Message => "Invalid user type '$Opts{UserType}'. Only 'User' and 'Customer' are valid.",
);
exit 1;
}
if ( $CommonObject{ConfigObject}->Get('SessionCheckRemoteIP') ) {
$CommonObject{LogObject}->Log(
Priority => 'error',
Message => "SysConfig 'SessionCheckRemoteIP' is enabled - this will lock SessionIDs out generated with this script.",
);
exit 1;
}
# get UserData for User/CustomerUser
my $UserObject = 'UserObject';
my $UserObjectFunction = 'GetUserData';
if ( $Opts{UserType} eq 'Customer' ) {
$UserObject = 'CustomerUserObject';
$UserObjectFunction = 'CustomerUserDataGet';
}
my %UserData = $CommonObject{ $UserObject }->$UserObjectFunction(
User => $Opts{UserLogin},
Valid => 1,
);
if ( !%UserData ) {
$CommonObject{LogObject}->Log(
Priority => 'error',
Message => "Error while getting user data for $Opts{UserType} '$Opts{UserLogin}'.",
);
exit 1;
}
# get groups rw/ro
for my $Type (qw(rw ro)) {
my $GroupObject = 'GroupObject';
if ( $Opts{UserType} eq 'Customer' ) {
$GroupObject = 'CustomerGroupObject';
}
my %GroupData = $CommonObject{ $GroupObject }->GroupMemberList(
Result => 'HASH',
Type => $Type,
UserID => $UserData{UserID},
);
for ( sort keys %GroupData ) {
if ( $Type eq 'rw' ) {
$UserData{"UserIsGroup[$GroupData{$_}]"} = 'Yes';
}
else {
$UserData{"UserIsGroupRo[$GroupData{$_}]"} = 'Yes';
}
}
}
# create new session id
my $NewSessionID = $CommonObject{SessionObject}->CreateSessionID(
%UserData,
UserLastRequest => $CommonObject{TimeObject}->SystemTime(),
UserType => $Opts{UserType},
);
if ( !$NewSessionID ) {
$CommonObject{LogObject}->Log(
Priority => 'error',
Message => "Error while generating SessionID for $Opts{UserType} '$Opts{UserLogin}'.",
);
exit 1;
}
my %URLConfigs = (
HttpType => '',
FQDN => '',
ScriptAlias => '',
SessionName => '',
CustomerPanelSessionName => '',
);
for my $ConfigName ( keys %URLConfigs ) {
$URLConfigs{ $ConfigName } = $CommonObject{ConfigObject}->Get( $ConfigName );
}
$URLConfigs{SessionID} = $NewSessionID;
my $URLStub = '<OTRS_CONFIG_HttpType>://<OTRS_CONFIG_FQDN>/<OTRS_CONFIG_ScriptAlias>';
if ( $Opts{UserType} eq 'User' ) {
$URLStub .= 'index.pl?<OTRS_CONFIG_SessionName>';
}
else {
$URLStub .= 'customer.pl?<OTRS_CONFIG_CustomerPanelSessionName>';
}
$URLStub .= '=<OTRS_CONFIG_SessionID>';
$URLStub =~ s{<OTRS_CONFIG_([^>]+)>}{$URLConfigs{ $1 }}gxms;
print $URLStub ."\n";
exit 0;
1;