cz.muni.fi.editor.services.commons.impl.EditorAccessVoterImpl.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.services.commons.impl.EditorAccessVoterImpl.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*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.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * 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 cz.muni.fi.editor.services.commons.impl;

import cz.muni.fi.editor.api.dto.UserDTO;
import cz.muni.fi.editor.api.support.MemberAuthority;
import cz.muni.fi.editor.api.support.OwnerAuthority;
import cz.muni.fi.editor.services.commons.security.EditorAccessVoter;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 10.8.2016.
 */
@Component(value = "editorVoter")
@Log4j2
@Transactional(readOnly = true)
public class EditorAccessVoterImpl implements EditorAccessVoter {
    @Override
    public boolean isMember(Long organizationID, UserDTO principal) {
        GrantedAuthority member = new MemberAuthority(organizationID);

        if (log.isDebugEnabled()) {
            log.debug("checking membership for {}", principal.getId());
            principal.getAuthorities().forEach(g -> log.debug("{} equals {} = {}", g, member, g.equals(member)));
        }
        return principal.getAuthorities().contains(member);
    }

    @Override
    public boolean isOwner(Long organizationID, UserDTO principal) {
        GrantedAuthority owner = new OwnerAuthority(organizationID);
        //        log.fatal("{} x {}",owner,principal.getAuthorities());
        return principal.getAuthorities().contains(owner);
    }

    @Override
    public boolean profile(Long target, UserDTO principal) {
        return target.equals(principal.getId());
    }
}