001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.administration;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.core.ui.Callable;
027
028/**
029 * This implementation creates a control allowing to affect a super user to a web context
030 */
031public class SuperUserClientSideElement extends org.ametys.runtime.plugins.admin.superuser.SuperUserClientSideElement implements Contextualizable
032{
033    private Context _context;
034    
035    @Override
036    public void contextualize(Context context) throws ContextException
037    {
038        _context = context;
039    }
040    
041    @Callable
042    @Override
043    public void affectUserToProfile(Map<String, String> user, String profileId, Map<String, Object> jsParameters)
044    {
045        String siteName = (String) jsParameters.get("siteName");
046        
047        // We need to set the site name as a request attribute, in order to WebJdbcProfileAssignmentStorage does its job correctly
048        Request request = ContextHelper.getRequest(_context);
049        request.setAttribute("siteName", siteName);
050        
051        try
052        {
053            super.affectUserToProfile(user, profileId, jsParameters);
054        }
055        finally
056        {
057            request.setAttribute("siteName", null);
058        }
059    }
060}