-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add email and display name header injection support - revised from PR #…
…30 (#124) * Initial commit * Apply suggestions from code review * Added tests * Fixed tests * Pre commit changes * fix: reformatting using mvn spotless:apply --------- Co-authored-by: Steve Boardwell <steve.boardwell@gmail.com>
- Loading branch information
1 parent
d5ad0c4
commit ad24059
Showing
6 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/jenkinsci/plugins/reverse_proxy_auth/data/ForwardedUserData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.jenkinsci.plugins.reverse_proxy_auth.data; | ||
|
||
import hudson.model.User; | ||
import hudson.tasks.Mailer; | ||
import java.io.IOException; | ||
|
||
/** | ||
* User data forwarded by the reverse proxy | ||
* **/ | ||
public class ForwardedUserData { | ||
/** Empty header may be a null string **/ | ||
private static final String NULL_HEADER = "(null)"; | ||
|
||
private String email; | ||
private String displayName; | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getDisplayName() { | ||
return displayName; | ||
} | ||
|
||
public void setDisplayName(String displayName) { | ||
this.displayName = displayName; | ||
} | ||
|
||
/** | ||
* Update the forwarded data to the jenkins user. | ||
* @return true if updated and saved | ||
* **/ | ||
public boolean update(User user) { | ||
boolean toReturn = false; | ||
if (updateDisplayName(user) || updateEmail(user)) { | ||
toReturn = true; | ||
try { | ||
user.save(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
return toReturn; | ||
} | ||
|
||
private boolean updateDisplayName(User user) { | ||
boolean toReturn = false; | ||
if (isNotNullHeader(displayName) && !displayName.equals(user.getFullName())) { | ||
user.setFullName(displayName); | ||
toReturn = true; | ||
} | ||
return toReturn; | ||
} | ||
|
||
private boolean updateEmail(User user) { | ||
boolean toReturn = false; | ||
if (isNotNullHeader(email)) { | ||
Mailer.UserProperty emailProp = user.getProperty(Mailer.UserProperty.class); | ||
if (emailProp == null || !email.equals(emailProp.getConfiguredAddress())) { | ||
emailProp = new Mailer.UserProperty(email); | ||
try { | ||
user.addProperty(emailProp); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
toReturn = true; | ||
} | ||
} | ||
return toReturn; | ||
} | ||
|
||
private static boolean isNotNullHeader(String value) { | ||
return value != null && !value.equals(NULL_HEADER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/org/jenkinsci/plugins/reverse_proxy_auth/data/ForwardedUserDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.jenkinsci.plugins.reverse_proxy_auth.data; | ||
|
||
import hudson.model.User; | ||
import hudson.tasks.Mailer; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.MockAuthorizationStrategy; | ||
|
||
public class ForwardedUserDataTest { | ||
private ForwardedUserData forwardedUserData; | ||
private User user; | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Before | ||
public void setup() { | ||
j.jenkins.setAuthorizationStrategy( | ||
new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().to("Max Mustermann")); | ||
|
||
forwardedUserData = new ForwardedUserData(); | ||
user = User.getOrCreateByIdOrFullName("Max Mustermann"); | ||
} | ||
|
||
@Test | ||
public void basicForwardedUserData() { | ||
forwardedUserData.setEmail("max.mustermann@example.com"); | ||
Assert.assertEquals("max.mustermann@example.com", forwardedUserData.getEmail()); | ||
|
||
forwardedUserData.setDisplayName("Max Mustermann"); | ||
Assert.assertEquals("Max Mustermann", forwardedUserData.getDisplayName()); | ||
} | ||
|
||
@Test | ||
public void testUpdate() { | ||
user.setFullName("John Doe"); | ||
forwardedUserData.setDisplayName("Max Mustermann"); | ||
forwardedUserData.update(user); | ||
Assert.assertEquals("Max Mustermann", user.getFullName()); | ||
|
||
forwardedUserData.setEmail("max.mustermann@example.com"); | ||
forwardedUserData.update(user); | ||
Mailer.UserProperty emailProp = user.getProperty(Mailer.UserProperty.class); | ||
Assert.assertEquals("max.mustermann@example.com", emailProp.getAddress()); | ||
} | ||
} |