Subversion Repositories SmartDukaan

Rev

Rev 35458 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.authentication;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.dtr.Api;
import com.spice.profitmandi.dao.entity.dtr.Role;
import com.spice.profitmandi.dao.entity.dtr.RoleApi;
import com.spice.profitmandi.dao.enumuration.dtr.Method;
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
import com.spice.profitmandi.dao.repository.dtr.ApiRepository;
import com.spice.profitmandi.dao.repository.dtr.RoleApiRepository;
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;

@Component
@Transactional(readOnly = true)
public class RoleManager {

        @Autowired
        private RoleApiRepository roleApiRepository;

        @Autowired
        private RoleRepository roleRepository;

        @Autowired
        private ApiRepository apiRepository;

        private static final Logger LOGGER = LogManager.getLogger(RoleManager.class);

        public boolean isAuthorizedURI(Set<Integer> roleIds, String contextPath, String uri, String method)
                        throws ProfitMandiBusinessException {
                if (isAdmin(roleIds)) {
                        LOGGER.debug("{}[{}]", uri, method);
                        return true;
                }
                List<RoleApi> roleApis = roleApiRepository.selectByRoleIds(roleIds);
                Set<Integer> apiIds = new HashSet<>();
                for (RoleApi roleApi : roleApis) {
                        apiIds.add(roleApi.getApiId());
                }
                List<Api> apis = apiRepository.selectByIds(apiIds);
                for (Api api : apis) {
                        if ((uri.matches(contextPath + api.getUri()) || (uri + "/").matches(contextPath + api.getUri())
                                        || uri.matches(contextPath + api.getUri() + "/"))
                                        && api.getMethod().equals(Method.valueOf(method))) {
                                return true;
                        }
                }
                throw new ProfitMandiBusinessException(ProfitMandiConstants.URI, uri + "[" + method + "]", "GE_1004");
        }

        public boolean isAdmin(Set<Integer> roleIds) {
                try {
                        Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.name());
                        return roleIds.contains(roleFofoAdmin.getId());
                } catch (Exception e) {
                        // This
                        return false;
                }
        }

        public boolean isPartner(Set<Integer> roleIds) {
                try {
                        Role rolePartner = roleRepository.selectByName(RoleType.FOFO.name());
                        return roleIds.contains(rolePartner.getId());
                } catch (Exception e) {
                        // This
                        return false;
                }
        }

        public boolean isRetailer(Set<Integer> roleIds) {
                try {
                        Role rolePartner = roleRepository.selectByName(RoleType.RETAILER.name());
                        return roleIds.contains(rolePartner.getId());
                } catch (Exception e) {
                        // This
                        return false;
                }
        }

        public boolean isUser(Set<Integer> roleIds) {
                try {
                        Role rolePartner = roleRepository.selectByName(RoleType.USER.name());
                        return roleIds.contains(rolePartner.getId());
                } catch (Exception e) {
                        // This
                        return false;
                }
        }
}