package jetbrains.buildServer.serverSide; import jetbrains.buildServer.users.SUser; import jetbrains.buildServer.users.User; import jetbrains.buildServer.users.UserModel; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * @author Eugene Petrenko * Created: 31.03.2010 18:17:14 */ public class UserContextFactory { private final UserModel myUserManager; public static final String ME = "me"; public UserContextFactory(UserModel userManager) { myUserManager = userManager; } public UserContext getUserNamesForUserSession(@Nullable final User sessionUser) { return new UserContext() { public boolean isMe(User user) { return user != null && sessionUser != null && user.getId() == sessionUser.getId(); } @NotNull public String getUserName(@Nullable User user) { if (usersAreEqual(sessionUser, user)) return ME; return UserContextFactory.this.getUserName(user); } }; } private boolean usersAreEqual(@Nullable User a, @Nullable User b) { return (a != null && b != null && a.getId() == b.getId()); } private String getUserName(@Nullable User user) { if (user == null) return "My Boss"; return user.getUsername(); } @Nullable public User findToUser(String userNameFormMessage) { final SUser user = myUserManager.findUserAccount(null, userNameFormMessage); if (user != null) return user; return null; } }