001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.s3;
020
021import java.io.IOException;
022import java.util.Iterator;
023import java.util.List;
024
025import lucee.commons.io.res.type.s3.AccessControl;
026import lucee.commons.io.res.type.s3.S3Exception;
027import lucee.commons.io.res.type.s3.S3Resource;
028import lucee.runtime.PageContext;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.Array;
032import lucee.runtime.type.ArrayImpl;
033import lucee.runtime.type.Collection;
034import lucee.runtime.type.KeyImpl;
035import lucee.runtime.type.Struct;
036import lucee.runtime.type.StructImpl;
037import lucee.runtime.type.util.KeyConstants;
038
039public class StoreGetACL extends S3Function {
040
041        public static final Collection.Key DISPLAY_NAME = KeyImpl.intern("displayName");
042        public static final Collection.Key PERMISSION = KeyImpl.intern("permission");
043        
044        
045        public static Object call(PageContext pc , String url) throws PageException {
046        
047                S3Resource res=toS3Resource(pc,url,"StoreGetACL");
048                try {
049                        return toArrayStruct(res.getAccessControlPolicy().getAccessControlList());
050                } catch (IOException e) {
051                        throw Caster.toPageException(e);
052                }
053    }
054
055        private static Object toArrayStruct(List<AccessControl> accessControlList) throws S3Exception {
056                Array arr=new ArrayImpl();
057                String type;
058                Struct sct;
059                AccessControl ac;
060                Iterator<AccessControl> it = accessControlList.iterator();
061                while(it.hasNext()){
062                        ac=it.next();
063                        arr.appendEL(sct=new StructImpl());
064                        sct.setEL(KeyConstants._id, ac.getId());
065                        sct.setEL(PERMISSION, ac.getPermission());
066                        
067                        type = AccessControl.toType(ac.getType());
068                        if("Group".equalsIgnoreCase(type))                              
069                                setGroup(sct,ac);
070                        else if("CanonicalUser".equalsIgnoreCase(type)) 
071                                sct.setEL(DISPLAY_NAME, ac.getDisplayName());
072                        else 
073                                sct.setEL(KeyConstants._email, ac.getId());
074                }
075                return arr;
076        }
077        
078        private static void setGroup(Struct sct, AccessControl ac) {
079                String uri = ac.getUri();
080                sct.setEL(KeyConstants._id, uri);
081                if("http://acs.amazonaws.com/groups/global/AllUsers".equalsIgnoreCase(uri))
082                        sct.setEL(KeyConstants._group, "all");
083                else if("http://acs.amazonaws.com/groups/global/AuthenticatedUsers".equalsIgnoreCase(uri))
084                        sct.setEL(KeyConstants._group, "authenticated");
085                else if("http://acs.amazonaws.com/groups/s3/LogDelivery".equalsIgnoreCase(uri))
086                        sct.setEL(KeyConstants._group, "log_delivery");
087        }
088
089}