SysGroupServiceImpl.java

/*
 * Copyright 2020 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gringlobal.service.impl;

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

import com.querydsl.jpa.impl.JPAQuery;
import lombok.extern.slf4j.Slf4j;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.QSysUser;
import org.gringlobal.model.SysGroup;
import org.gringlobal.model.SysUser;
import org.gringlobal.persistence.SysGroupRepository;
import org.gringlobal.persistence.SysUserRepository;
import org.gringlobal.service.SysGroupService;
import org.gringlobal.service.filter.SysGroupFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Maxym Borodenko
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class SysGroupServiceImpl extends FilteredCRUDServiceImpl<SysGroup, SysGroupFilter, SysGroupRepository> implements SysGroupService {

	@Autowired
	private SysUserRepository sysUserRepository;

	@Autowired(required = false)
	private CacheManager cacheManager;

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public SysGroup create(SysGroup source) {
		SysGroup sysGroup = new SysGroup(source.getGroupTag());
		sysGroup.setIsEnabled("Y");

		SysGroup saved = repository.save(sysGroup);
		return _lazyLoad(saved);
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public SysGroup update(SysGroup input, SysGroup target) {
		log.debug("Set enabled to {} for {}", input.getIsEnabled(), input);
		target.setIsEnabled(input.getIsEnabled());

		SysGroup saved = repository.save(target);
		return _lazyLoad(saved);
	}

	@Override
	public Page<SysUser> listMembers(SysGroup sysGroup, Pageable page) {

		JPAQuery<SysUser> query = jpaQueryFactory.selectFrom(QSysUser.sysUser)
			// join cooperator
			.leftJoin(QSysUser.sysUser.cooperator()).fetchAll()
			// predicate
			.where(QSysUser.sysUser.groupMaps.any().sysGroup().eq(sysGroup));

		return sysUserRepository.findAll(query, page);
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public SysGroup enable(Long id) {
		final SysGroup loaded = load(id);
		loaded.setIsEnabled("Y");
		return _lazyLoad(repository.save(loaded));
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	public SysGroup disable(Long id) {
		final SysGroup loaded = load(id);
		if (SysGroup.ADMIN_GROUPTAG.equals(loaded.getGroupTag())) {
			throw new SecurityException("Cannot disable ADMINS group");
		}
		loaded.setIsEnabled("N");
		SysGroup saved = repository.save(loaded);
		return _lazyLoad(saved);
	}

	@Override
	@Transactional
	@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#sysGroup, 'ADMINISTRATION')")
	public SysGroup updateGroupTag(SysGroup sysGroup, String groupTag) {
		final SysGroup loaded = load(sysGroup.getId());
		if (SysGroup.ADMIN_GROUPTAG.equals(loaded.getGroupTag())) {
			throw new SecurityException("Cannot change ADMINS group tag");
		}
		if (loaded.getGroupTag().equals(groupTag)) {
			return loaded;
		}
		loaded.setGroupTag(groupTag);
		SysGroup saved = repository.save(loaded);

		clearAclCache();

		return _lazyLoad(saved);
	}

	private void clearAclCache() {
		if (this.cacheManager != null) {
			Cache aclCache = this.cacheManager.getCache("aclCache");
			if (aclCache != null) {
				aclCache.clear();
			}
		}
	}

	@Override
	public List<SysGroup> listAllByIds(Set<Long> ids) {
		return repository.findAllById(ids);
	}

	@Override
	public Page<SysGroup> list(SysGroupFilter filter, Pageable page) throws SearchException {
		return super.list(SysGroup.class, filter, page);
	}
}