下面列出了怎么用hudson.model.UserProperty的API类实例代码及写法,或者点击链接到github查看源代码。
private static Collection<UserWithPassword> getter(HudsonPrivateSecurityRealm target) {
return target.getAllUsers().stream()
.map(u -> {
UserWithPassword user = new UserWithPassword(u.getId(), null);
user.setName(u.getFullName());
user.setDescription(u.getDescription());
List<UserProperty> properties = u.getAllProperties()
.stream()
.filter(userProperty -> !userProperty.getClass().getName().equals("com.cloudbees.plugins.credentials.UserCredentialsProvider$UserCredentialsProperty"))
.collect(Collectors.toList());
user.setProperties(properties);
return user;
})
.collect(Collectors.toList());
}
public TestGitRepo(String name, File tmpDir, TaskListener listener) throws IOException, InterruptedException {
this.name = name;
this.listener = listener;
envVars = new EnvVars();
gitDir = tmpDir;
User john = User.get(johnDoe.getName(), true);
UserProperty johnsMailerProperty = new Mailer.UserProperty(johnDoe.getEmailAddress());
john.addProperty(johnsMailerProperty);
User jane = User.get(janeDoe.getName(), true);
UserProperty janesMailerProperty = new Mailer.UserProperty(janeDoe.getEmailAddress());
jane.addProperty(janesMailerProperty);
// initialize the git interface.
gitDirPath = new FilePath(gitDir);
git = Git.with(listener, envVars).in(gitDir).getClient();
// finally: initialize the repo
git.init();
}
private static void setter(HudsonPrivateSecurityRealm target, Collection<UserWithPassword> value) throws IOException {
for (UserWithPassword user : value) {
User updatedUser = createAccount(target, user);
updatedUser.setFullName(user.name);
updatedUser.setDescription(user.description);
if (user.getProperties() != null) {
for (UserProperty property : user.getProperties()) {
updatedUser.addProperty(property);
}
}
}
}
@Override
public SecurityComponents createSecurityComponents() {
return new SecurityComponents(
new AuthenticationManager() {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (authentication instanceof AnonymousAuthenticationToken)
return authentication;
throw new BadCredentialsException("Unexpected authentication type: " + authentication);
}
},
new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
// Retrieve the OicUserProperty to get the list of groups that has to be set in the OicUserDetails object.
LOGGER.fine("loadUserByUsername in createSecurityComponents called, username: " + username);
User u = User.get(username, false, Collections.emptyMap());
if (u == null) {
LOGGER.fine("loadUserByUsername in createSecurityComponents called, no user '" + username + "' found");
throw new UsernameNotFoundException(username);
}
LOGGER.fine("loadUserByUsername in createSecurityComponents called, user: " + u);
List<UserProperty> props = u.getAllProperties();
LOGGER.fine("loadUserByUsername in createSecurityComponents called, number of props: " + props.size());
GrantedAuthority[] auths = new GrantedAuthority[0];
for (UserProperty prop: props) {
LOGGER.fine("loadUserByUsername in createSecurityComponents called, prop of type: " + prop.getClass().toString());
if (prop instanceof OicUserProperty) {
OicUserProperty oicProp = (OicUserProperty) prop;
LOGGER.fine("loadUserByUsername in createSecurityComponents called, oic prop found with username: " + oicProp.getUserName());
auths = oicProp.getAuthoritiesAsGrantedAuthorities();
LOGGER.fine("loadUserByUsername in createSecurityComponents called, oic prop with auths size: " + auths.length);
}
}
return new OicUserDetails(username, auths);
}
}
);
}
private UsernamePasswordAuthenticationToken loginAndSetUserData(String userName, IdToken idToken, GenericJson userInfo) throws IOException {
GrantedAuthority[] grantedAuthorities = determineAuthorities(idToken, userInfo);
if(LOGGER.isLoggable(Level.FINEST)) {
StringBuilder grantedAuthoritiesAsString = new StringBuilder("(");
for(GrantedAuthority grantedAuthority : grantedAuthorities) {
grantedAuthoritiesAsString.append(" ").append(grantedAuthority.getAuthority());
}
grantedAuthoritiesAsString.append(" )");
LOGGER.finest("GrantedAuthorities:" + grantedAuthoritiesAsString);
}
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, "", grantedAuthorities);
SecurityContextHolder.getContext().setAuthentication(token);
User user = User.get(token.getName());
// Store the list of groups in a OicUserProperty so it can be retrieved later for the UserDetails object.
user.addProperty(new OicUserProperty(userName, grantedAuthorities));
if(emailFieldName!=null) {
String email = userInfo == null ? getField(idToken, emailFieldName) : (String) getField(userInfo, emailFieldName);
if (email != null) {
user.addProperty(new Mailer.UserProperty(email));
}
}
if(fullNameFieldName!=null) {
String fullName = userInfo == null ? getField(idToken, fullNameFieldName) : (String) getField(userInfo, fullNameFieldName);
if (fullName != null) {
user.setFullName(fullName);
}
}
OicUserDetails userDetails = new OicUserDetails(userName, grantedAuthorities);
SecurityListener.fireAuthenticated(userDetails);
return token;
}
@DataBoundSetter
public void setProperties(List<UserProperty> properties) {
this.properties = properties;
}
public List<UserProperty> getProperties() {
return properties;
}
@Override
public UserProperty newInstance(User user) {
LOGGER.fine("OicUserPropertyDescriptor.newInstance called, user:" + user);
return new OicUserProperty(user.getId(), new GrantedAuthority[0]);
}
@Override
public UserProperty newInstance(User user) {
return new DingTalkUserProperty(null);
}
@Override
public UserProperty newInstance(@Nullable StaplerRequest req, @Nonnull JSONObject formData) {
return new DingTalkUserProperty(formData.optString("mobile"));
}