it.scoppelletti.programmerpower.security.User.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.programmerpower.security.User.java

Source

/*
 * Copyright (C) 2011 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * 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 it.scoppelletti.programmerpower.security;

import java.io.*;
import java.util.*;
import javax.persistence.*;
import org.springframework.security.core.*;
import org.springframework.security.core.authority.*;
import org.springframework.security.core.userdetails.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.data.*;
import it.scoppelletti.programmerpower.reflect.*;
import it.scoppelletti.programmerpower.types.*;

/**
 * Utente.
 * 
 * @see   it.scoppelletti.programmerpower.security.UserManager
 * @since 1.0.0
 */
@Entity
@Table(name = "sec_user")
@TableGenerator(name = "sec_user.id", table = TableGeneration.TABLE, pkColumnName = TableGeneration.PKCOLUMNNAME, pkColumnValue = "sec_user.id", valueColumnName = TableGeneration.VALUECOLUMNNAME, initialValue = TableGeneration.INITIALVALUE, allocationSize = TableGeneration.ALLOCATIONSIZE)
@Final
public class User implements Serializable, OptimisticLockSupport, UserDetails {
    private static final long serialVersionUID = 1L;

    /**
     * @serial Id&#46; dell&rsquo;entit&agrave;.
     */
    private Integer myId;

    /**
     * @serial Codice.
     */
    private String myCode;

    /**
     * @serial Nome.
     */
    private String myName;

    /**
     * @serial Password.
     */
    private String myPwd;

    /**
     * @serial Indicatore di utente abilitato.
     */
    private boolean myEnabled;

    /**
     * @serial Ruoli.
     */
    private Set<String> myRoles = new HashSet<String>();

    /**
     * @serial Autorit&agrave;.
     */
    private Set<GrantedAuthority> myAuthorities;

    /**
     * @serial Numero di versione.
     */
    private int myVersion;

    /**
     * Costruttore.
     */
    public User() {
    }

    /**
     * Costruttore.
     * 
     * @param code Codice dell&rsquo;utente.
     */
    public User(String code) {
        if (Strings.isNullOrEmpty(code)) {
            throw new ArgumentNullException("code");
        }

        myCode = code.toLowerCase();
    }

    /**
     * Restituisce l&rsquo;id&#46; dell&rsquo;entit&agrave;.
     * 
     * @return Valore.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "sec_user.id")
    @Column(name = "id", nullable = false)
    public Integer getId() {
        return myId;
    }

    /**
     * Imposta l&rsquo;id&#46. dell&rsquo;entit&agrave;.
     * 
     * @param value Valore.
     */
    @SuppressWarnings("unused")
    private void setId(Integer value) {
        myId = value;
    }

    /**
     * Restituisce il codice dell&rsquo;utente.
     * 
     * @return Valore.
     */
    @org.hibernate.annotations.NaturalId
    @Column(name = "code", nullable = false, length = 16)
    public String getCode() {
        return myCode;
    }

    /**
     * Imposta il codice dell&rsquo;utente.
     * 
     * @param value Valore.
     */
    @SuppressWarnings("unused")
    private void setCode(String value) {
        myCode = value;
    }

    /**
     * Restituisce il codice per l&rsquo;autenticazione.
     * 
     * @return Valore.
     * @see    #getCode
     */
    @Transient
    public String getUsername() {
        return myCode;
    }

    /**
     * Restituisce il nome.
     * 
     * @return Valore.
     * @see    #setName
     */
    @Column(name = "name", length = 255)
    public String getName() {
        return myName;
    }

    /**
     * Imposta il nome.
     * 
     * @param value Valore.
     * @see         #getName
     */
    public void setName(String value) {
        myName = value;
    }

    /**
     * Restituisce la password.
     * 
     * @return Valore.
     * @see    #setPassword
     */
    @Column(name = "pwd", nullable = false, length = 64)
    public String getPassword() {
        return myPwd;
    }

    /**
     * Imposta la password.
     * 
     * @param value Valore.
     * @see         #getPassword
     */
    public void setPassword(String value) {
        myPwd = value;
    }

    /**
     * Indica se l&rsquo;utente &egrave; abilitato.
     * 
     * @return Indicatore.
     * @see    #setEnabled
     */
    @Column(name = "enabled", nullable = false)
    @org.hibernate.annotations.Type(type = "org.hibernate.type.TrueFalseType")
    public boolean isEnabled() {
        return myEnabled;
    }

    /**
     * Imposta l&rsquo;indicatore di utente abilitato.
     * 
     * @param value Valore.
     * @see         #isEnabled
     */
    public void setEnabled(boolean value) {
        myEnabled = value;
    }

    /**
     * Indica se l&rsquo;account non &egrave; scaduto.
     * 
     * @return {@code true}
     */
    @Transient
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * Indica se l&rsquo;account non &egrave; bloccato.
     * 
     * @return {@code true}
     */
    @Transient
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * Indica se le credenziali dell&rsquo;utente non sono scadute.
     * 
     * @return {@code true}
     */
    @Transient
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * Restituisce i ruoli.
     * 
     * @return Collezione.
     * @see    #getAuthorities
     * @see    it.scoppelletti.programmerpower.security.RoleManager
     */
    @ElementCollection
    @JoinTable(name = "sec_user_role", joinColumns = @JoinColumn(name = "user_id"))
    @Column(name = "role", nullable = false, length = 255)
    @org.hibernate.annotations.ForeignKey(name = "FK_sec_user_role")
    public Set<String> getRoles() {
        return myRoles;
    }

    /**
     * Imposta i ruoli.
     * 
     * @param obj Collezione.
     */
    @SuppressWarnings("unused")
    private void setRoles(Set<String> obj) {
        myRoles = obj;
    }

    /**
     * Restituisce le autorit&agrave;.
     * 
     * @return Collezione.
     * @see    #getRoles
     */
    @Transient
    public Collection<GrantedAuthority> getAuthorities() {
        return myAuthorities;
    }

    /**
     * Imposta le autorit&agrave;
     */
    @PostLoad
    @PrePersist
    @PreUpdate
    @Transient
    private void setAuthorities() {
        Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();

        for (String role : myRoles) {
            roles.add(new SimpleGrantedAuthority(role));
        }

        myAuthorities = Collections.unmodifiableSet(roles);
    }

    @Version
    @Column(name = "version", nullable = false)
    public int getVersion() {
        return myVersion;
    }

    /**
     * Imposta il numero di versione.
     * 
     * @param value Valore.
     */
    @SuppressWarnings("unused")
    private void setVersion(int value) {
        myVersion = value;
    }

    /**
     * Rappresenta l&rsquo;oggetto con una stringa.
     * 
     * @return Stringa.
     */
    @Override
    public String toString() {
        return myCode;
    }

    /**
     * Verifica l&rsquo;uguaglianza con un altro oggetto.
     * 
     * @param  obj Oggetto di confronto.
     * @return     Esito della verifica. 
     */
    @Override
    public boolean equals(Object obj) {
        User op;

        if (!(obj instanceof User)) {
            return false;
        }

        op = (User) obj;

        if (!Strings.equals(myCode, op.getCode(), true)) {
            return false;
        }

        return true;
    }

    /**
     * Calcola il codice hash dell&rsquo;oggetto.
     * 
     * @return Valore.
     */
    @Override
    public int hashCode() {
        int value = 17;

        if (myCode != null) {
            value = 37 * myCode.toLowerCase().hashCode();
        }

        return value;
    }
}