diff --git a/src/main/java/hudson/plugins/im/IMConnectionProvider.java b/src/main/java/hudson/plugins/im/IMConnectionProvider.java index 1be1934..81b914c 100644 --- a/src/main/java/hudson/plugins/im/IMConnectionProvider.java +++ b/src/main/java/hudson/plugins/im/IMConnectionProvider.java @@ -8,7 +8,10 @@ import java.util.logging.Level; import java.util.logging.Logger; +import jenkins.model.Jenkins; import org.acegisecurity.Authentication; +import org.acegisecurity.userdetails.UsernameNotFoundException; +//import org.springframework.security.core.userdetails.UsernameNotFoundException; /** * Abstract implementation of a provider of {@link IMConnection}s. @@ -132,9 +135,20 @@ public Authentication getAuthentication() { return null; } - User u = User.get(descriptor.getHudsonUserName()); + if (descriptor.getHudsonUserName().isBlank() && !(Jenkins.getInstance().isUseSecurity())) { + return null; + } - return u.impersonate(); + try { + User u = User.get(descriptor.getHudsonUserName()); + return u.impersonate(); + } catch (UsernameNotFoundException ue) { + if (descriptor.getHudsonUserName().isBlank()) { + throw new UsernameNotFoundException( + "No local Jenkins user name is configured for instant messaging to act as"); + } + throw ue; + } } }; } diff --git a/src/main/java/hudson/plugins/im/bot/Bot.java b/src/main/java/hudson/plugins/im/bot/Bot.java index d51be02..ff8c2e6 100644 --- a/src/main/java/hudson/plugins/im/bot/Bot.java +++ b/src/main/java/hudson/plugins/im/bot/Bot.java @@ -26,6 +26,7 @@ import jenkins.model.Jenkins; import jenkins.security.NotReallyRoleSensitiveCallable; +import org.acegisecurity.userdetails.UsernameNotFoundException; /** * Instant messaging bot. @@ -162,15 +163,20 @@ public void onMessage(final IMMessage msg) { final BotCommand command = this.cmdsAndAliases.get(cmd); if (command != null) { if (isAuthenticationNeeded()) { - ACL.impersonate(this.authentication.getAuthentication(), new NotReallyRoleSensitiveCallable() { - private static final long serialVersionUID = 1L; - - @Override - public Void call() throws IMException { - command.executeCommand(Bot.this, chat, msg, s, args); - return null; - } - }); + try { + ACL.impersonate(this.authentication.getAuthentication(), new NotReallyRoleSensitiveCallable() { + private static final long serialVersionUID = 1L; + + @Override + public Void call() throws IMException { + command.executeCommand(Bot.this, chat, msg, s, args); + return null; + } + }); + } catch (UsernameNotFoundException ue) { + this.chat.sendMessage(s.getNickname() + " This bot is not authorized to execute commands at this time, sorry!"); + throw ue; + } } else { command.executeCommand(Bot.this, chat, msg, s, args); }