hudson.security.ACL.as()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中hudson.security.ACL.as()方法的一些代码示例,展示了ACL.as()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ACL.as()方法的具体详情如下:
包路径:hudson.security.ACL
类名称:ACL
方法名:as

ACL.as介绍

[英]Changes the Authentication associated with the current thread to the specified one and returns an AutoCloseable that restores the previous security context.

This makes impersonation much easier within code as it can now be used using the try with resources construct:

try (ACLContext ctx = ACL.as(auth)) { 
... 
}

[中]将与当前线程关联的身份验证更改为指定的身份验证,并返回可自动关闭的身份验证,以恢复以前的安全上下文。
这使得代码中的模拟更加容易,因为现在可以使用try with resources构造来使用它:

try (ACLContext ctx = ACL.as(auth)) { 
... 
}

代码示例

代码示例来源:origin: jenkinsci/jenkins

@Override
  public V call() throws Exception {
    try (ACLContext ctxt = ACL.as(authentication)) {
      return r.call();
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    try (ACLContext ctxt = ACL.as(authentication)) {
      r.run();
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

@Override
  public V call() throws Exception {
    try (ACLContext ctxt = ACL.as(authentication)) {
      return r.call();
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    try (ACLContext ctxt = ACL.as(authentication)) {
      r.run();
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

@Override
public void onLocationChanged(final Item item, final String oldFullName, final String newFullName) {
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    locationChanged(oldFullName, newFullName);
  }
}
private void locationChanged(String oldFullName, String newFullName) {

代码示例来源:origin: jenkinsci/jenkins

@Override
public void onDeleted(final Item item) {
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    deleted(item);
  }
}
private void deleted(Item item) {

代码示例来源:origin: jenkinsci/jenkins

@Override
public void onLocationChanged(final Item item, final String oldName, final String newName) {
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    locationChanged(item, oldName, newName);
  }
}
private void locationChanged(Item item, String oldName, String newName) {

代码示例来源:origin: jenkinsci/jenkins

@Override
public void onLocationChanged(final Item item, final String oldFullName, final String newFullName) {
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    locationChanged(item, oldFullName, newFullName);
  }
}
private void locationChanged(Item item, String oldFullName, String newFullName) {

代码示例来源:origin: jenkinsci/jenkins

/**
 * Changes the {@link Authentication} associated with the current thread to the specified one and returns an
 * {@link AutoCloseable} that restores the previous security context.
 *
 * <p>
 * This makes impersonation much easier within code as it can now be used using the try with resources construct:
 * <pre>
 *     try (ACLContext ctx = ACL.as(auth)) {
 *        ...
 *     }
 * </pre>
 *
 * @param user the user to impersonate.
 * @return the previous authentication context
 * @since 2.14
 */
@Nonnull
public static ACLContext as(@CheckForNull User user) {
  return as(user == null ? Jenkins.ANONYMOUS : user.impersonate());
}

代码示例来源:origin: jenkinsci/jenkins

public void contextDestroyed(ServletContextEvent event) {
  try (ACLContext old = ACL.as(ACL.SYSTEM)) {
    Jenkins instance = Jenkins.getInstanceOrNull();
    try {
      if (instance != null) {
        instance.cleanUp();
      }
    } catch (Exception e) {
      LOGGER.log(Level.SEVERE, "Failed to clean up. Restart will continue.", e);
    }
    terminated = true;
    Thread t = initThread;
    if (t != null && t.isAlive()) {
      LOGGER.log(Level.INFO, "Shutting down a Jenkins instance that was still starting up", new Throwable("reason"));
      t.interrupt();
    }
    // Logger is in the system classloader, so if we don't do this
    // the whole web app will never be undeployed.
    Logger.getLogger("").removeHandler(handler);
  } finally {
    JenkinsJVMAccess._setJenkinsJVM(false);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    boolean failures = false;
    INSTALLING: while (true) {
      try {
        updateCenter.persistInstallStatus();
        Thread.sleep(500);
        failures = false;
        for (Future<UpdateCenter.UpdateCenterJob> jobFuture : installJobs) {
          if(!jobFuture.isDone() && !jobFuture.isCancelled()) {
            continue INSTALLING;
          }
          UpdateCenter.UpdateCenterJob job = jobFuture.get();
          if(job instanceof InstallationJob && ((InstallationJob)job).status instanceof DownloadJob.Failure) {
            failures = true;
          }
        }
      } catch (Exception e) {
        LOGGER.log(WARNING, "Unexpected error while waiting for initial plugin set to install.", e);
      }
      break;
    }
    updateCenter.persistInstallStatus();
    if(!failures) {
      try (ACLContext acl = ACL.as(currentAuth)) {
        InstallUtil.proceedToNextStateFrom(InstallState.INITIAL_PLUGINS_INSTALLING);
      }
    }
  }
}.start();

代码示例来源:origin: jenkinsci/jenkins

/**
 * Securely check for the existence of an item before trying to create one with the same name.
 * @param parent the folder where we are about to create/rename/move an item
 * @param newName the proposed new name
 * @param variant if not null, an existing item which we accept could be there
 * @throws IllegalArgumentException if there is already something there, which you were supposed to know about
 * @throws Failure if there is already something there but you should not be told details
 */
static void verifyItemDoesNotAlreadyExist(@Nonnull ItemGroup<?> parent, @Nonnull String newName, @CheckForNull Item variant) throws IllegalArgumentException, Failure {
  Item existing;
  try (ACLContext ctxt = ACL.as(ACL.SYSTEM)) {
    existing = parent.getItem(newName);
  }
  if (existing != null && existing != variant) {
    if (existing.hasPermission(Item.DISCOVER)) {
      String prefix = parent.getFullName();
      throw new IllegalArgumentException((prefix.isEmpty() ? "" : prefix + "/") + newName + " already exists");
    } else {
      // Cannot hide its existence, so at least be as vague as possible.
      throw new Failure("");
    }
  }
}

代码示例来源:origin: jenkinsci/jenkins

private Map<Job,Collection<ReverseBuildTrigger>> calculateCache() {
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    final Map<Job, Collection<ReverseBuildTrigger>> result = new WeakHashMap<>();
    for (Job<?, ?> downstream : Jenkins.getInstance().allItems(Job.class)) {
      ReverseBuildTrigger trigger =
          ParameterizedJobMixIn.getTrigger(downstream, ReverseBuildTrigger.class);
      if (trigger == null) {
        continue;
      }
      List<Job> upstreams =
          Items.fromNameList(downstream.getParent(), trigger.getUpstreamProjects(), Job.class);
      LOGGER.log(Level.FINE, "from {0} see upstreams {1}", new Object[]{downstream, upstreams});
      for (Job upstream : upstreams) {
        if (upstream instanceof AbstractProject && downstream instanceof AbstractProject) {
          continue; // handled specially
        }
        Collection<ReverseBuildTrigger> triggers = result.get(upstream);
        if (triggers == null) {
          triggers = new LinkedList<>();
          result.put(upstream, triggers);
        }
        triggers.remove(trigger);
        triggers.add(trigger);
      }
    }
    return result;
  }
}

代码示例来源:origin: jenkinsci/jenkins

public synchronized void run() {
  if (!(status instanceof Pending)) {
    return;
  }
  status = new Running();
  try {
    // safeRestart records the current authentication for the log, so set it to the managing user
    try (ACLContext acl = ACL.as(User.get(authentication, false, Collections.emptyMap()))) {
      Jenkins.getInstance().safeRestart();
    }
  } catch (RestartNotSupportedException exception) {
    // ignore if restart is not allowed
    status = new Failure();
    error = exception;
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void onLocationChanged(Item item, final String oldFullName, final String newFullName) {
    try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
      for (Job<?, ?> p : Jenkins.getInstance().allItems(Job.class)) {
        ReverseBuildTrigger t = ParameterizedJobMixIn.getTrigger(p, ReverseBuildTrigger.class);
        if (t != null) {
          String revised =
              Items.computeRelativeNamesAfterRenaming(oldFullName, newFullName,
                  t.getUpstreamProjects(), p.getParent());
          if (!revised.equals(t.upstreamProjects)) {
            t.upstreamProjects = revised;
            try {
              p.save();
            } catch (IOException e) {
              LOGGER.log(Level.WARNING,
                  "Failed to persist project setting during rename from " + oldFullName + " to "
                      + newFullName, e);
            }
          }
        }
      }
    }
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override public int parseArguments(Parameters params) throws CmdLineException {
  final Jenkins j = Jenkins.get();
  final String src = params.getParameter(0);
  T s = j.getItemByFullName(src, type());
  if (s == null) {
    final Authentication who = Jenkins.getAuthentication();
    try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
      Item actual = j.getItemByFullName(src);
      if (actual == null) {
        LOGGER.log(Level.FINE, "really no item exists named {0}", src);
      } else {
        LOGGER.log(Level.WARNING, "running as {0} could not find {1} of {2}", new Object[] {who.getPrincipal(), actual, type()});
      }
    }
    T nearest = Items.findNearest(type(), src, j);
    if (nearest != null) {
      throw new IllegalArgumentException("No such job '" + src + "'; perhaps you meant '" + nearest.getFullName() + "'?");
    } else {
      throw new IllegalArgumentException("No such job '" + src + "'");
    }
  }
  setter.addValue(s);
  return 1;
}

代码示例来源:origin: jenkinsci/jenkins

throw new Failure(Messages.AbstractItem_NewNameInUse(newName));
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
  item = getParent().getItem(newName);
  if (item != null) {

代码示例来源:origin: jenkinsci/jenkins

try (ACLContext context = ACL.as(ACL.SYSTEM)) {
  LOGGER.info("Attempting to dynamic load "+arc);
  PluginWrapper p = null;

代码示例来源:origin: jenkinsci/jenkins

try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
  final Item itemBySystemUser = jenkins.getItemByFullName(fullName);
  if (itemBySystemUser == null) {

代码示例来源:origin: jenkinsci/configuration-as-code-plugin

private void configureWith(Mapping entries) throws ConfiguratorException {
  // Check input before actually applying changes,
  // so we don't let master in a weird state after some ConfiguratorException has been thrown
  final Mapping clone = entries.clone();
  checkWith(clone);
  final ObsoleteConfigurationMonitor monitor = ObsoleteConfigurationMonitor.get();
  monitor.reset();
  ConfigurationContext context = new ConfigurationContext(registry);
  context.addListener(monitor::record);
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    invokeWith(entries, (configurator, config) -> configurator.configure(config, context));
  }
}

相关文章

微信公众号

最新文章

更多