- # If the user logged in is the SCO user and he tries to go out the SCO module, log the user out removing the CGISESSID cookie
- if ( $in->{type} eq 'opac' and $in->{template_name} !~ m|sco/| ) {
- if ( C4::Context->preference('AutoSelfCheckID') && $user eq C4::Context->preference('AutoSelfCheckID') ) {
- $template = C4::Templates::gettemplate( 'opac-auth.tt', 'opac', $in->{query} );
- my $cookie = $in->{query}->cookie(
+ if ( $in->{type} eq 'opac' && $user ) {
+ my $kick_out;
+
+ if (
+# If the user logged in is the SCO user and they try to go out of the SCO module,
+# log the user out removing the CGISESSID cookie
+ $in->{template_name} !~ m|sco/|
+ && C4::Context->preference('AutoSelfCheckID')
+ && $user eq C4::Context->preference('AutoSelfCheckID')
+ )
+ {
+ $kick_out = 1;
+ }
+ elsif (
+# If the user logged in is the SCI user and they try to go out of the SCI module,
+# kick them out unless it is SCO with a valid permission
+# or they are a superlibrarian
+ $in->{template_name} !~ m|sci/|
+ && haspermission( $user, { self_check => 'self_checkin_module' } )
+ && !(
+ $in->{template_name} =~ m|sco/| && haspermission(
+ $user, { self_check => 'self_checkout_module' }
+ )
+ )
+ && $flags && $flags->{superlibrarian} != 1
+ )
+ {
+ $kick_out = 1;
+ }
+
+ if ($kick_out) {
+ $template = C4::Templates::gettemplate( 'opac-auth.tt', 'opac',
+ $in->{query} );
+ $cookie = $in->{query}->cookie(