##@file # menu for lemonldap::ng portal ##@class # menu class for lemonldap::ng portal package Lemonldap::NG::Portal::Menu; use strict; use warnings; use Lemonldap::NG::Portal::Simple; use Lemonldap::NG::Portal::_LibAccess; use base qw(Lemonldap::NG::Portal::_LibAccess); use Clone qw(clone); our $VERSION = '1.4.0'; our $catlevel = 0; ## @method void menuInit() # Prepare menu template elements # @return nothing sub menuInit { my $self = shift; $self->{apps}->{imgpath} ||= '/apps/'; # Modules to display $self->{menuModules} ||= "Appslist ChangePassword LoginHistory Logout"; $self->{menuDisplayModules} = $self->displayModules(); # Extract password from POST data $self->{oldpassword} = $self->param('oldpassword'); $self->{newpassword} = $self->param('newpassword'); $self->{confirmpassword} = $self->param('confirmpassword'); $self->{dn} = $self->{sessionInfo}->{dn}; $self->{user} = $self->{sessionInfo}->{_user}; # Try to change password $self->{menuError} = $self->_subProcess( qw(passwordDBInit modifyPassword passwordDBFinish sendPasswordMail)) unless $self->{ignorePasswordChange}; # Default menu error code $self->{menuError} = PE_PASSWORD_OK if ( $self->{passwordWasChanged} ); $self->{menuError} ||= $self->{error}; # Tab to display # Get the tab URL parameter $self->{menuDisplayTab} = $self->param("tab") || "none"; # Default to appslist if invalid tab URL parameter $self->{menuDisplayTab} = "appslist" unless ( $self->{menuDisplayTab} =~ /^(password|logout|loginHistory)$/ ); # Force password tab in case of password error $self->{menuDisplayTab} = "password" if ( ( scalar( grep { $_ == $self->{menuError} } ( 25, #PE_PP_CHANGE_AFTER_RESET 26, #PE_PP_PASSWORD_MOD_NOT_ALLOWED 27, #PE_PP_MUST_SUPPLY_OLD_PASSWORD 28, #PE_PP_INSUFFICIENT_PASSWORD_QUALITY 29, #PE_PP_PASSWORD_TOO_SHORT 30, #PE_PP_PASSWORD_TOO_YOUNG 31, #PE_PP_PASSWORD_IN_HISTORY 32, #PE_PP_GRACE 33, #PE_PP_EXP_WARNING 34, #PE_PASSWORD_MISMATCH 39, #PE_BADOLDPASSWORD 74, #PE_MUST_SUPPLY_OLD_PASSWORD ) ) ) ); # Application list for old templates if ( $self->{useOldMenuItems} ) { $self->{menuAppslistMenu} = $self->appslistMenu(); $self->{menuAppslistDesc} = $self->appslistDescription(); } return; } ## @method arrayref displayModules() # List modules that can be displayed in Menu # @return modules list sub displayModules { my $self = shift; my $displayModules = []; # Modules list my @modules = split( /\s/, $self->{menuModules} ); # Foreach module, eval condition # Store module in result if condition is valid foreach my $module (@modules) { my $cond = $self->{ 'portalDisplay' . $module }; $cond = 1 unless defined $cond; $self->lmLog( "Evaluate condition $cond for module $module", 'debug' ); if ( $self->safe->reval($cond) ) { my $moduleHash = { $module => 1 }; $moduleHash->{'APPSLIST_LOOP'} = $self->appslist() if ( $module eq 'Appslist' ); if ( $module eq 'LoginHistory' ) { $moduleHash->{'SUCCESS_LOGIN'} = $self->mkSessionArray( $self->{sessionInfo}->{loginHistory}->{successLogin}, "", 0, 0 ); $moduleHash->{'FAILED_LOGIN'} = $self->mkSessionArray( $self->{sessionInfo}->{loginHistory}->{failedLogin}, "", 0, 1 ); } push @$displayModules, $moduleHash; } } return $displayModules; } ## @method arrayref appslist() # Returns categories and applications list as HTML::Template loop # @return categories and applications list sub appslist { my ($self) = splice @_; my $appslist = []; return $appslist unless defined $self->{applicationList}; # Reset level $catlevel = 0; my $applicationList = clone( $self->{applicationList} ); my $filteredList = $self->_filter($applicationList); push @$appslist, $self->_buildCategoryHash( "", $filteredList, $catlevel ); # We must return an ARRAY ref return ( ref $appslist->[0]->{categories} eq "ARRAY" ) ? $appslist->[0]->{categories} : []; } ## @method private hashref _buildCategoryHash(string catname,hashref cathash, int catlevel) # Build hash for a category # @param catname Category name # @param cathash Hash of category elements # @param catlevel Category level # @return Category Hash sub _buildCategoryHash { my ( $self, $catid, $cathash, $catlevel ) = splice @_; my $catname = $cathash->{catname} || $catid; my $applications; my $categories; # Extract applications from hash my $apphash; foreach my $catkey ( sort keys %$cathash ) { next if $catkey =~ /(type|options|catname)/; if ( $cathash->{$catkey}->{type} eq "application" ) { $apphash->{$catkey} = $cathash->{$catkey}; } } # Display applications first if ( scalar keys %$apphash > 0 ) { foreach my $appkey ( sort keys %$apphash ) { push @$applications, $self->_buildApplicationHash( $appkey, $apphash->{$appkey} ); } } # Display subcategories foreach my $catkey ( sort keys %$cathash ) { next if $catkey =~ /(type|options|catname)/; if ( $cathash->{$catkey}->{type} eq "category" ) { push @$categories, $self->_buildCategoryHash( $catkey, $cathash->{$catkey}, $catlevel + 1 ); } } my $categoryHash = { category => 1, catname => $catname, catid => $catid, catlevel => $catlevel }; $categoryHash->{applications} = $applications if $applications; $categoryHash->{categories} = $categories if $categories; return $categoryHash; } ## @method private hashref _buildApplicationHash(string appid, hashref apphash) # Build hash for an application # @param $appid Application ID # @param $apphash Hash of application elements # @return Application Hash sub _buildApplicationHash { my ( $self, $appid, $apphash ) = splice @_; my $applications; # Get application items my $appname = $apphash->{options}->{name} || $appid; my $appuri = $apphash->{options}->{uri} || ""; my $appdesc = $apphash->{options}->{description}; my $applogo = $apphash->{options}->{logo}; # Detect sub applications my $subapphash; foreach my $key ( sort keys %$apphash ) { next if $key =~ /(type|options|catname)/; if ( $apphash->{$key}->{type} eq "application" ) { $subapphash->{$key} = $apphash->{$key}; } } # Display sub applications if ( scalar keys %$subapphash > 0 ) { foreach my $appkey ( sort keys %$subapphash ) { push @$applications, $self->_buildApplicationHash( $appkey, $subapphash->{$appkey} ); } } my $applicationHash = { application => 1, appname => $appname, appuri => $appuri, appdesc => $appdesc, applogo => $applogo, appid => $appid, }; $applicationHash->{applications} = $applications if $applications; return $applicationHash; } ## @method string appslistMenu() # Returns HTML code for application list menu. # @return HTML string sub appslistMenu { my $self = shift; # We no more use XML file for menu configuration unless ( defined $self->{applicationList} ) { $self->abort( "XML menu configuration is deprecated", "Please use lmMigrateConfFiles2ini to migrate your menu configuration" ); } # Use configuration to get menu parameters my $applicationList = clone( $self->{applicationList} ); my $filteredList = $self->_filter($applicationList); return $self->_displayConfCategory( "", $filteredList, $catlevel ); } ## @method string appslistDescription() # Returns HTML code for application description. # @return HTML string sub appslistDescription { my $self = shift; # We no more use XML file for menu configuration unless ( defined $self->{applicationList} ) { $self->lmLog( "XML menu configuration is deprecated. Please use lmMigrateConfFiles2ini to migrate your menu configuration", 'error' ); return " "; } # Use configuration to get menu parameters my $applicationList = clone( $self->{applicationList} ); return $self->_displayConfDescription( "", $applicationList ); } ## @method string _displayConfCategory(string catname, hashref cathash, int catlevel) # Creates and returns HTML code for a category. # @param catname Category name # @param cathash Hash of category elements # @param catlevel Category level # @return HTML string sub _displayConfCategory { my ( $self, $catname, $cathash, $catlevel ) = splice @_; my $html; my $key; # Init HTML list $html .= "
$appname
\n" if defined $appname; $html .= "$appdesc
\n" if defined $appdesc; $html .= "