User.java 17.9 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt, Tom Huybrechts
K
kohsuke 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
K
kohsuke 已提交
24 25
package hudson.model;

26
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
K
kohsuke 已提交
27
import com.thoughtworks.xstream.XStream;
K
kohsuke 已提交
28
import hudson.CopyOnWrite;
K
kohsuke 已提交
29
import hudson.FeedAdapter;
30
import hudson.Functions;
K
kohsuke 已提交
31
import hudson.Util;
32
import hudson.XmlFile;
33
import hudson.BulkChange;
34
import hudson.tasks.Mailer;
K
kohsuke 已提交
35
import hudson.model.Descriptor.FormException;
36
import hudson.model.listeners.SaveableListener;
37 38 39
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.security.Permission;
K
kohsuke 已提交
40 41
import hudson.util.RunList;
import hudson.util.XStream2;
42 43
import net.sf.json.JSONObject;

44 45
import org.acegisecurity.Authentication;
import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
K
kohsuke 已提交
46 47
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
48 49
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
50
import org.apache.commons.io.filefilter.DirectoryFileFilter;
K
kohsuke 已提交
51 52

import javax.servlet.ServletException;
53
import javax.servlet.http.HttpServletResponse;
K
kohsuke 已提交
54 55
import java.io.File;
import java.io.IOException;
56
import java.io.FileFilter;
K
kohsuke 已提交
57 58
import java.util.ArrayList;
import java.util.Calendar;
59
import java.util.Collection;
K
kohsuke 已提交
60
import java.util.Collections;
61 62
import java.util.HashSet;
import java.util.Iterator;
K
kohsuke 已提交
63 64
import java.util.List;
import java.util.Map;
65
import java.util.Set;
66
import java.util.TreeMap;
K
kohsuke 已提交
67 68 69 70 71
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Represents a user.
K
kohsuke 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 *
 * <p>
 * In Hudson, {@link User} objects are created in on-demand basis;
 * for example, when a build is performed, its change log is computed
 * and as a result commits from users who Hudson has never seen may be discovered.
 * When this happens, new {@link User} object is created.
 *
 * <p>
 * If the persisted record for an user exists, the information is loaded at
 * that point, but if there's no such record, a fresh instance is created from
 * thin air (this is where {@link UserPropertyDescriptor#newInstance(User)} is
 * called to provide initial {@link UserProperty} objects.
 *
 * <p>
 * Such newly created {@link User} objects will be simply GC-ed without
 * ever leaving the persisted record, unless {@link User#save()} method
 * is explicitly invoked (perhaps as a result of a browser submitting a
 * configuration.)
 *
 *
K
kohsuke 已提交
92 93
 * @author Kohsuke Kawaguchi
 */
K
kohsuke 已提交
94
@ExportedBean
95
public class User extends AbstractModelObject implements AccessControlled, Saveable, Comparable<User> {
K
kohsuke 已提交
96 97 98 99 100 101 102 103 104 105

    private transient final String id;

    private volatile String fullName;

    private volatile String description;

    /**
     * List of {@link UserProperty}s configured for this project.
     */
K
kohsuke 已提交
106
    @CopyOnWrite
K
kohsuke 已提交
107 108 109
    private volatile List<UserProperty> properties = new ArrayList<UserProperty>();


110
    private User(String id, String fullName) {
K
kohsuke 已提交
111
        this.id = id;
112
        this.fullName = fullName;
K
kohsuke 已提交
113 114
        load();
    }
K
kohsuke 已提交
115

116 117 118 119
    public int compareTo(User that) {
        return this.id.compareTo(that.id);
    }

K
kohsuke 已提交
120 121 122 123
    /**
     * Loads the other data from disk if it's available.
     */
    private synchronized void load() {
124 125
        properties.clear();

K
kohsuke 已提交
126 127 128 129 130 131 132 133
        XmlFile config = getConfigFile();
        try {
            if(config.exists())
                config.unmarshal(this);
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "Failed to load "+config,e);
        }

134 135 136 137 138 139
        // remove nulls that have failed to load
        for (Iterator<UserProperty> itr = properties.iterator(); itr.hasNext();) {
            if(itr.next()==null)
                itr.remove();            
        }

K
kohsuke 已提交
140 141
        // allocate default instances if needed.
        // doing so after load makes sure that newly added user properties do get reflected
142
        for (UserPropertyDescriptor d : UserProperty.all()) {
K
kohsuke 已提交
143 144 145 146 147 148 149
            if(getProperty(d.clazz)==null) {
                UserProperty up = d.newInstance(this);
                if(up!=null)
                    properties.add(up);
            }
        }

K
kohsuke 已提交
150 151 152 153
        for (UserProperty p : properties)
            p.setUser(this);
    }

K
kohsuke 已提交
154
    @Exported
K
kohsuke 已提交
155 156 157 158 159
    public String getId() {
        return id;
    }

    public String getUrl() {
160
        return "user/"+Util.rawEncode(id);
161 162 163
    }

    public String getSearchUrl() {
164
        return "/user/"+Util.rawEncode(id);
K
kohsuke 已提交
165 166
    }

K
kohsuke 已提交
167 168 169
    /**
     * The URL of the user page.
     */
K
kohsuke 已提交
170
    @Exported(visibility=999)
K
kohsuke 已提交
171
    public String getAbsoluteUrl() {
172
        return Hudson.getInstance().getRootUrl()+getUrl();
K
kohsuke 已提交
173 174
    }

K
kohsuke 已提交
175 176 177 178 179 180 181
    /**
     * Gets the human readable name of this user.
     * This is configurable by the user.
     *
     * @return
     *      never null.
     */
K
kohsuke 已提交
182
    @Exported(visibility=999)
K
kohsuke 已提交
183 184 185 186
    public String getFullName() {
        return fullName;
    }

K
kohsuke 已提交
187 188 189 190 191 192 193 194
    /**
     * Sets the human readable name of thie user.
     */
    public void setFullName(String name) {
        if(Util.fixEmptyAndTrim(name)==null)    name=id;
        this.fullName = name;
    }

K
kohsuke 已提交
195
    @Exported
K
kohsuke 已提交
196 197 198 199 200 201 202 203 204 205 206
    public String getDescription() {
        return description;
    }

    /**
     * Gets the user properties configured for this user.
     */
    public Map<Descriptor<UserProperty>,UserProperty> getProperties() {
        return Descriptor.toMap(properties);
    }

207 208 209 210 211 212 213 214 215
    /**
     * Updates the user object by adding a property.
     */
    public synchronized void addProperty(UserProperty p) throws IOException {
        UserProperty old = getProperty(p.getClass());
        List<UserProperty> ps = new ArrayList<UserProperty>(properties);
        if(old!=null)
            ps.remove(old);
        ps.add(p);
216
        p.setUser(this);
217 218 219
        properties = ps;
        save();
    }
220

K
kohsuke 已提交
221
    /**
222
     * List of all {@link UserProperty}s exposed primarily for the remoting API.
K
kohsuke 已提交
223
     */
K
kohsuke 已提交
224
    @Exported(name="property",inline=true)
225 226 227
    public List<UserProperty> getAllProperties() {
        return Collections.unmodifiableList(properties);
    }
K
kohsuke 已提交
228
    
K
kohsuke 已提交
229 230 231 232 233 234
    /**
     * Gets the specific property, or null.
     */
    public <T extends UserProperty> T getProperty(Class<T> clazz) {
        for (UserProperty p : properties) {
            if(clazz.isInstance(p))
235
                return clazz.cast(p);
K
kohsuke 已提交
236 237 238 239 240 241 242 243
        }
        return null;
    }

    /**
     * Accepts the new description.
     */
    public synchronized void doSubmitDescription( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
244
        checkPermission(Hudson.ADMINISTER);
K
kohsuke 已提交
245 246 247 248 249 250 251 252
        req.setCharacterEncoding("UTF-8");

        description = req.getParameter("description");
        save();
        
        rsp.sendRedirect(".");  // go to the top page
    }

K
kohsuke 已提交
253 254 255 256 257
    /**
     * Gets the fallback "unknown" user instance.
     * <p>
     * This is used to avoid null {@link User} instance.
     */
258 259 260
    public static User getUnknown() {
        return get("unknown");
    }
K
kohsuke 已提交
261

K
kohsuke 已提交
262
    /**
263
     * Gets the {@link User} object by its id or full name.
K
kohsuke 已提交
264 265 266 267 268 269 270
     *
     * @param create
     *      If true, this method will never return null for valid input
     *      (by creating a new {@link User} object if none exists.)
     *      If false, this method will return null if {@link User} object
     *      with the given name doesn't exist.
     */
271 272
    public static User get(String idOrFullName, boolean create) {
        if(idOrFullName==null)
K
kohsuke 已提交
273
            return null;
274 275
        String id = idOrFullName.replace('\\', '_').replace('/', '_').replace('<','_')
                                .replace('>','_');  // 4 replace() still faster than regex
276 277
        if (Functions.isWindows()) id = id.replace(':','_');

K
kohsuke 已提交
278
        synchronized(byName) {
K
kohsuke 已提交
279
            User u = byName.get(id);
280 281 282 283 284
            if(u==null) {
                User tmp = new User(id, idOrFullName);
                if (create || tmp.getConfigFile().exists()) {
                    byName.put(id,u=tmp);
                }
K
kohsuke 已提交
285 286 287 288 289
            }
            return u;
        }
    }

K
kohsuke 已提交
290
    /**
291
     * Gets the {@link User} object by its id or full name.
K
kohsuke 已提交
292
     */
293 294
    public static User get(String idOrFullName) {
        return get(idOrFullName,true);
K
kohsuke 已提交
295 296
    }

K
kohsuke 已提交
297 298 299 300 301 302 303 304 305
    /**
     * Gets the {@link User} object representing the currently logged-in user, or null
     * if the current user is anonymous.
     * @since 1.172
     */
    public static User current() {
        Authentication a = Hudson.getAuthentication();
        if(a instanceof AnonymousAuthenticationToken)
            return null;
306
        return get(a.getName());
K
kohsuke 已提交
307 308
    }

309
    private static volatile long lastScanned;
310

K
kohsuke 已提交
311 312 313 314
    /**
     * Gets all the users.
     */
    public static Collection<User> getAll() {
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        if(System.currentTimeMillis() -lastScanned>10000) {
            // occasionally scan the file system to check new users
            // whether we should do this only once at start up or not is debatable.
            // set this right away to avoid another thread from doing the same thing while we do this.
            // having two threads doing the work won't cause race condition, but it's waste of time.
            lastScanned = System.currentTimeMillis();

            File[] subdirs = getRootDir().listFiles((FileFilter)DirectoryFileFilter.INSTANCE);
            if(subdirs==null)       return Collections.emptyList(); // shall never happen

            for (File subdir : subdirs)
                if(new File(subdir,"config.xml").exists())
                    User.get(subdir.getName());

            lastScanned = System.currentTimeMillis();
        }

K
kohsuke 已提交
332 333 334 335 336
        synchronized (byName) {
            return new ArrayList<User>(byName.values());
        }
    }

337 338 339 340 341 342 343 344 345
    /**
     * Reloads the configuration from disk.
     */
    public static void reload() {
        // iterate over an array to be concurrency-safe
        for( User u : byName.values().toArray(new User[0]) )
            u.load();
    }

346 347 348 349 350 351 352
    /**
     * Stop gap hack. Don't use it. To be removed in the trunk.
     */
    public static void clear() {
        byName.clear();
    }

K
kohsuke 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365
    /**
     * Returns the user name.
     */
    public String getDisplayName() {
        return getFullName();
    }

    /**
     * Gets the list of {@link Build}s that include changes by this user,
     * by the timestamp order.
     * 
     * TODO: do we need some index for this?
     */
366 367
    @WithBridgeMethods(List.class)
    public RunList getBuilds() {
K
kohsuke 已提交
368
        List<AbstractBuild> r = new ArrayList<AbstractBuild>();
369 370 371 372
        for (AbstractProject<?,?> p : Hudson.getInstance().getAllItems(AbstractProject.class))
            for (AbstractBuild<?,?> b : p.getBuilds())
                if(b.hasParticipant(this))
                    r.add(b);
373
        return RunList.fromRuns(r);
K
kohsuke 已提交
374 375
    }

376 377 378 379 380 381 382 383 384 385 386 387
    /**
     * Gets all the {@link AbstractProject}s that this user has committed to.
     * @since 1.191
     */
    public Set<AbstractProject<?,?>> getProjects() {
        Set<AbstractProject<?,?>> r = new HashSet<AbstractProject<?,?>>();
        for (AbstractProject<?,?> p : Hudson.getInstance().getAllItems(AbstractProject.class))
            if(p.hasParticipant(this))
                r.add(p);
        return r;
    }

388
    public @Override String toString() {
K
kohsuke 已提交
389 390 391 392 393 394 395
        return fullName;
    }

    /**
     * The file we save our configuration.
     */
    protected final XmlFile getConfigFile() {
396 397 398 399 400 401 402 403
        return new XmlFile(XSTREAM,new File(getRootDir(),id +"/config.xml"));
    }

    /**
     * Gets the directory where Hudson stores user information.
     */
    private static File getRootDir() {
        return new File(Hudson.getInstance().getRootDir(), "users");
K
kohsuke 已提交
404 405 406 407 408 409
    }

    /**
     * Save the settings to a file.
     */
    public synchronized void save() throws IOException {
410
        if(BulkChange.contains(this))   return;
411
        getConfigFile().write(this);
412
        SaveableListener.fireOnChange(this, getConfigFile());
K
kohsuke 已提交
413 414
    }

415 416 417 418 419 420 421 422 423 424 425 426 427
    /**
     * Deletes the data directory and removes this user from Hudson.
     *
     * @throws IOException
     *      if we fail to delete.
     */
    public synchronized void delete() throws IOException {
        synchronized (byName) {
            byName.remove(id);
            Util.deleteRecursive(new File(getRootDir(), id));
        }
    }

K
kohsuke 已提交
428 429 430 431 432 433 434
    /**
     * Exposed remote API.
     */
    public Api getApi() {
        return new Api(this);
    }

K
kohsuke 已提交
435 436 437
    /**
     * Accepts submission from the configuration page.
     */
438
    public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, FormException {
439
        checkPermission(Hudson.ADMINISTER);
K
kohsuke 已提交
440 441 442

        req.setCharacterEncoding("UTF-8");

443 444
        fullName = req.getParameter("fullName");
        description = req.getParameter("description");
445

446
        JSONObject json = req.getSubmittedForm();
K
kohsuke 已提交
447

448 449 450 451 452 453 454 455 456 457
        List<UserProperty> props = new ArrayList<UserProperty>();
        int i = 0;
        for (UserPropertyDescriptor d : UserProperty.all()) {
            JSONObject o = json.getJSONObject("userProperty" + (i++));
            UserProperty p = getProperty(d.clazz);
            if (p != null) {
                p = p.reconfigure(req, o);
            } else {
                p = d.newInstance(req, o);
            }
K
kohsuke 已提交
458

459 460
            p.setUser(this);
            props.add(p);
K
kohsuke 已提交
461
        }
462 463 464 465 466
        this.properties = props;

        save();

        rsp.sendRedirect(".");
K
kohsuke 已提交
467 468
    }

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    /**
     * Deletes this user from Hudson.
     */
    public void doDoDelete(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
        requirePOST();
        checkPermission(Hudson.ADMINISTER);
        if (id.equals(Hudson.getAuthentication().getName())) {
            rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Cannot delete self");
            return;
        }

        delete();

        rsp.sendRedirect2("../..");
    }

K
kohsuke 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    public void doRssAll( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
        rss(req, rsp, " all builds", RunList.fromRuns(getBuilds()));
    }

    public void doRssFailed( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
        rss(req, rsp, " regression builds", RunList.fromRuns(getBuilds()).regressionOnly());
    }

    private void rss(StaplerRequest req, StaplerResponse rsp, String suffix, RunList runs) throws IOException, ServletException {
        RSS.forwardToRss(getDisplayName()+ suffix, getUrl(),
            runs.newBuilds(), FEED_ADAPTER, req, rsp );
    }


    /**
K
kohsuke 已提交
500 501
     * Keyed by {@link User#id}. This map is used to ensure
     * singleton-per-id semantics of {@link User} objects.
K
kohsuke 已提交
502
     */
503
    private static final Map<String,User> byName = new TreeMap<String,User>(String.CASE_INSENSITIVE_ORDER);
K
kohsuke 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

    /**
     * Used to load/save user configuration.
     */
    private static final XStream XSTREAM = new XStream2();

    private static final Logger LOGGER = Logger.getLogger(User.class.getName());

    static {
        XSTREAM.alias("user",User.class);
    }

    /**
     * {@link FeedAdapter} to produce build status summary in the feed.
     */
    public static final FeedAdapter<Run> FEED_ADAPTER = new FeedAdapter<Run>() {
        public String getEntryTitle(Run entry) {
            return entry+" : "+entry.getBuildStatusSummary().message;
        }

        public String getEntryUrl(Run entry) {
            return entry.getUrl();
        }

        public String getEntryID(Run entry) {
            return "tag:"+entry.getParent().getName()+':'+entry.getId();
        }

532 533 534 535 536
        public String getEntryDescription(Run entry) {
            // TODO: provide useful details
            return null;
        }

K
kohsuke 已提交
537 538 539
        public Calendar getEntryTimestamp(Run entry) {
            return entry.getTimestamp();
        }
540 541

        public String getEntryAuthor(Run entry) {
542
            return Mailer.descriptor().getAdminAddress();
543
        }
K
kohsuke 已提交
544
    };
545 546 547


    public ACL getACL() {
548
        final ACL base = Hudson.getInstance().getAuthorizationStrategy().getACL(this);
549
        // always allow a non-anonymous user full control of himself.
550 551
        return new ACL() {
            public boolean hasPermission(Authentication a, Permission permission) {
552 553
                return (a.getName().equals(id) && !(a instanceof AnonymousAuthenticationToken))
                        || base.hasPermission(a, permission);
554 555
            }
        };
556 557
    }

K
TAB->WS  
kohsuke 已提交
558 559 560
    public void checkPermission(Permission permission) {
        getACL().checkPermission(permission);
    }
561

K
TAB->WS  
kohsuke 已提交
562 563 564
    public boolean hasPermission(Permission permission) {
        return getACL().hasPermission(permission);
    }
565

566 567 568 569 570 571 572 573
    /**
     * With ADMINISTER permission, can delete users with persisted data but can't delete self.
     */
    public boolean canDelete() {
        return hasPermission(Hudson.ADMINISTER) && !id.equals(Hudson.getAuthentication().getName())
                && new File(getRootDir(), id).exists();
    }

574 575 576 577 578 579 580 581 582 583
    public Object getDynamic(String token) {
        for (UserProperty property: getProperties().values()) {
            if (property instanceof Action) {
                Action a= (Action) property;
            if(a.getUrlName().equals(token) || a.getUrlName().equals('/'+token))
                return a;
            }
        }
        return null;
    }
584
}