All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.core.lang.UserInfo Maven / Gradle / Ivy

package com.soento.core.lang;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.soento.core.enums.DataPermissionType;
import com.soento.core.util.CollectionUtil;
import lombok.Getter;
import lombok.Setter;

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

/**
 * @author soento
 */
@Getter
@Setter
public class UserInfo extends BaseObject {
    /**
     * 用户头像
     */
    private String avatar;
    /**
     * 用户Id
     */
    private String id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 用户昵称
     */
    private String nickname;
    /**
     * 角色集合
     */
    private List roles;
    /**
     * 部门集合
     */
    private List departments;
    /**
     * 数据权限类型
     */
    private Integer dataPermissionType = DataPermissionType.ALL.getValue();

    public boolean isRoot() {
        Set roleIds = getRoleIds();
        return roleIds != null && roleIds.size() > 0 && roleIds.contains(RoleInfo.ROOT);
    }

    @JsonIgnore
    public Set getRoleIds() {
        Set roleIds = new HashSet<>();
        if (roles != null && roles.size() > 0) {
            for (RoleInfo role : roles) {
                roleIds.add(role.getId());
            }
        }
        return roleIds;
    }

    @JsonIgnore
    public List getRoleNames() {
        List roleNames = new ArrayList<>();
        if (roles != null && roles.size() > 0) {
            for (RoleInfo role : roles) {
                roleNames.add(role.getName());
            }
        }
        return roleNames;
    }

    @JsonIgnore
    public List getFunctionPermissions() {
        List functionPermissions = new ArrayList<>();
        Set ids = new HashSet<>();
        if (roles != null && roles.size() > 0) {
            for (RoleInfo role : roles) {
                if (CollectionUtil.isNotEmpty(role.getFunctionPermissions())) {
                    for (FunctionPermission function : role.getFunctionPermissions()) {
                        if (!ids.contains(function.getId())) {
                            functionPermissions.add(function);
                        }
                        ids.add(function.getId());
                    }
                }
            }
        }
        return functionPermissions;
    }

    @JsonIgnore
    public Set getFunctionNames() {
        Set names = new HashSet<>();
        if (CollectionUtil.isNotEmpty(roles)) {
            for (RoleInfo role : roles) {
                if (CollectionUtil.isNotEmpty(role.getFunctionPermissions())) {
                    for (FunctionPermission function : role.getFunctionPermissions()) {
                        names.add(function.getName());
                    }
                }
            }
        }
        return names;
    }

    @JsonIgnore
    public Set getFunctionUris() {
        Set uris = new HashSet<>();
        if (CollectionUtil.isNotEmpty(roles)) {
            for (RoleInfo role : roles) {
                if (CollectionUtil.isNotEmpty(role.getFunctionPermissions())) {
                    for (FunctionPermission function : role.getFunctionPermissions()) {
                        uris.add(function.getUri());
                    }
                }
            }
        }
        return uris;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy