User.java 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
N
nzomkxia 已提交
17
package org.apache.dubbo.admin.model.domain;
M
min 已提交
18

N
nzomkxia 已提交
19
import org.apache.dubbo.admin.common.util.ParseUtils;
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

import java.util.Arrays;
import java.util.List;

/**
 * User
 *
 */
public class User extends Entity {

    public static final String REALM = "dubbo";
    public static final String ROOT = "R";
    public static final String ADMINISTRATOR = "A";
    public static final String MANAGER = "M";
    public static final String GUEST = "G";
    public static final String ANONYMOUS = "anonymous";
    public static final String LEGACY = "legacy";
    private static final long serialVersionUID = 7330539198581235339L;
    private String username;

    private String password;

    private String role;

    private String creator;

    private boolean enabled;

    private String name;

    private String department;

    private String email;

    private String phone;

    private String alitalk;

    private String locale;

    private String servicePrivilege;

    private List<String> servicePrivileges;

    public User() {
    }

    public User(Long id) {
        super(id);
    }

    public static boolean isValidPrivilege(String servicePrivilege) {
        if (servicePrivilege == null || servicePrivilege.length() == 0) {
            return true;
        }
        String[] privileges = servicePrivilege.trim().split("\\s*,\\s*");
        for (String privilege : privileges) {
            if (privilege.endsWith("*")) {
                privilege = privilege.substring(0, privilege.length() - 1);
            }
            if (privilege.indexOf('*') > -1) {
                return false;
            }
        }
        return true;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean hasServicePrivilege(String[] services) {
        if (services == null || services.length == 0)
            throw new IllegalArgumentException("services == null");
        for (String service : services) {
            boolean r = hasServicePrivilege(service);
            if (!r)
                return false;
        }
        return true;
    }

    public boolean canGrantPrivilege(String servicePrivilege) {
        if (servicePrivilege == null || servicePrivilege.length() == 0) {
            return true;
        }
        if (servicePrivileges == null || servicePrivileges.size() == 0) {
            return false;
        }
        String[] privileges = servicePrivilege.trim().split("\\s*,\\s*");
        for (String privilege : privileges) {
            boolean hasPrivilege = false;
            for (String ownPrivilege : servicePrivileges) {
                if (matchPrivilege(ownPrivilege, privilege)) {
                    hasPrivilege = true;
                }
            }
            if (!hasPrivilege) {
                return false;
            }
        }
        return true;
    }

    private boolean matchPrivilege(String ownPrivilege, String privilege) {
        if ("*".equals(ownPrivilege) || ownPrivilege.equals(privilege)) {
            return true;
        }
        if (privilege.endsWith("*")) {
            if (!ownPrivilege.endsWith("*")) {
                return false;
            }
            privilege = privilege.substring(0, privilege.length() - 1);
            ownPrivilege = ownPrivilege.substring(0, ownPrivilege.length() - 1);
            return privilege.startsWith(ownPrivilege);
        } else {
            if (ownPrivilege.endsWith("*")) {
                ownPrivilege = ownPrivilege.substring(0, ownPrivilege.length() - 1);
            }
            return privilege.startsWith(ownPrivilege);
        }
    }

    public boolean hasServicePrivilege(String service) {
        if (service == null || service.length() == 0)
            return false;
        if (role == null || GUEST.equalsIgnoreCase(role)) {
            return false;
        }
        if (ROOT.equalsIgnoreCase(role)) {
            return true;
        }

        if (servicePrivileges != null && servicePrivileges.size() > 0) {
            for (String privilege : servicePrivileges) {
                boolean ok = ParseUtils.isMatchGlobPattern(privilege, service);
                if (ok) {
                    return true;
                }
            }
        }
        return false;
    }

    public String getServicePrivilege() {
        return servicePrivilege;
    }

    public void setServicePrivilege(String servicePrivilege) {
        this.servicePrivilege = servicePrivilege;
        if (servicePrivilege != null && servicePrivilege.length() > 0) {
            servicePrivileges = Arrays.asList(servicePrivilege.trim().split("\\s*,\\s*"));
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAlitalk() {
        return alitalk;
    }

    public void setAlitalk(String alitalk) {
        this.alitalk = alitalk;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

}