001package com.identityworksllc.iiq.common.auth;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006import java.util.Locale;
007import java.util.TimeZone;
008
009import sailpoint.api.DynamicScopeMatchmaker;
010import sailpoint.api.SailPointContext;
011import sailpoint.object.Capability;
012import sailpoint.object.Filter;
013import sailpoint.object.Identity;
014import sailpoint.object.QueryOptions;
015import sailpoint.object.SailPointObject;
016import sailpoint.tools.GeneralException;
017import sailpoint.web.UserContext;
018
019/**
020 * Dummy authorization context for use with the authorization methods
021 */
022public class DummyAuthContext implements UserContext {
023        
024        /**
025         * The current SailPointContext
026         */
027        private SailPointContext context;
028        
029        /**
030         * The identity name
031         */
032        private String identityName;
033        
034        /**
035         * Constructor
036         * 
037         * @param context The current IIQ context
038         * @param identityName The identity name
039         */
040        public DummyAuthContext(SailPointContext context, String identityName) {
041                this.context = context;
042                this.identityName = identityName;
043        }
044
045        @Override
046        public SailPointContext getContext() {
047                return context;
048        }
049
050        @Override
051        public Locale getLocale() {
052                return Locale.getDefault();
053        }
054
055        @Override
056        public Identity getLoggedInUser() throws GeneralException {
057                return context.getObjectByName(Identity.class, identityName);
058        }
059
060        @Override
061        public List<Capability> getLoggedInUserCapabilities() {
062                try {
063                        return getLoggedInUser().getCapabilityManager().getEffectiveCapabilities();
064                } catch(GeneralException e) {
065                        return new ArrayList<Capability>();
066                }
067        }
068
069        @Override
070        public List<String> getLoggedInUserDynamicScopeNames() throws GeneralException {
071                DynamicScopeMatchmaker matchmaker = new DynamicScopeMatchmaker(context);
072                return matchmaker.getMatches(getLoggedInUser());
073        }
074
075        @Override
076        public String getLoggedInUserName() throws GeneralException {
077                return identityName;
078        }
079
080        @Override
081        public Collection<String> getLoggedInUserRights() {
082                try {
083                        return getLoggedInUser().getCapabilityManager().getEffectiveFlattenedRights();
084                } catch(GeneralException e) {
085                        return new ArrayList<String>();
086                }
087        }
088
089        @Override
090        public TimeZone getUserTimeZone() {
091                return TimeZone.getDefault();
092        }
093
094        /**
095         * Introduced in 8.1
096         * @return Always false
097         */
098        public boolean isMobileLogin() {
099                return false;
100        }
101
102        @Override
103        public boolean isObjectInUserScope(SailPointObject object) throws GeneralException {
104                return isObjectInUserScope(object.getId(), object.getClass());
105        }
106
107        @Override
108        public boolean isObjectInUserScope(String id, @SuppressWarnings("rawtypes") Class clazz) throws GeneralException {
109              QueryOptions scopingOptions = new QueryOptions();
110              scopingOptions.setScopeResults(Boolean.valueOf(true));
111              scopingOptions.add(new Filter[] { Filter.eq("id", id) });
112              
113              @SuppressWarnings("unchecked") 
114              int count = getContext().countObjects(clazz, scopingOptions);
115              
116              return (id == null) || ("".equals(id)) || count > 0;
117        }
118
119        @Override
120        public boolean isScopingEnabled() throws GeneralException {
121                // TODO Auto-generated method stub
122                return false;
123        }
124
125}