de.kaiserpfalzEdv.office.ui.web.security.KPOfficeUserDetail.java Source code

Java tutorial

Introduction

Here is the source code for de.kaiserpfalzEdv.office.ui.web.security.KPOfficeUserDetail.java

Source

/*
 * Copyright 2015 Kaiserpfalz EDV-Service, Roland T. Lichti
 *
 * 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 de.kaiserpfalzEdv.office.ui.web.security;

import de.kaiserpfalzEdv.office.core.security.OfficeLoginTicket;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.HashSet;

/**
 * @author klenkes <rlichti@kaiserpfalz-edv.de>
 * @version 0.1.0
 * @since 0.1.0
 */
public class KPOfficeUserDetail implements UserDetails {
    private static final long serialVersionUID = -7718480665472015448L;

    private OfficeLoginTicket ticket;

    public KPOfficeUserDetail(@NotNull final OfficeLoginTicket ticket) {
        this.ticket = ticket;
    }

    public OfficeLoginTicket getTicket() {
        return ticket;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        HashSet<GrantedAuthority> result = new HashSet<>(ticket.getEntitlements().size());

        ticket.getEntitlements().forEach(e -> result.add(new KPOfficeGrantedAuthority(e)));

        return result;
    }

    @Override
    public String getPassword() {
        return null; //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getUsername() {
        return ticket.getAccountName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        KPOfficeUserDetail rhs = (KPOfficeUserDetail) obj;
        return new EqualsBuilder().append(this.ticket, rhs.ticket).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(ticket).toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("ticket", ticket.getTicketId())
                .append("account", ticket.getAccountName()).toString();
    }
}