| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>DomainPermission.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.authz.permission</a> > <span class="el_source">DomainPermission.java</span></div><h1>DomainPermission.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.authz.permission; |
| |
| import org.apache.shiro.util.StringUtils; |
| |
| import java.util.Set; |
| |
| /** |
| * Provides a base Permission class from which type-safe/domain-specific subclasses may extend. Can be used |
| * as a base class for JPA/Hibernate persisted permissions that wish to store the parts of the permission string |
| * in separate columns (e.g. 'domain', 'actions' and 'targets' columns), which can be used in querying |
| * strategies. |
| * |
| * @since 1.0 |
| */ |
| public class DomainPermission extends WildcardPermission { |
| |
| private String domain; |
| private Set<String> actions; |
| private Set<String> targets; |
| |
| private static final long serialVersionUID = 1l; |
| |
| /** |
| * Creates a domain permission with *all* actions for *all* targets; |
| */ |
| <span class="fc" id="L44"> public DomainPermission() {</span> |
| <span class="fc" id="L45"> this.domain = getDomain(getClass());</span> |
| <span class="fc" id="L46"> setParts(getDomain(getClass()));</span> |
| <span class="fc" id="L47"> }</span> |
| |
| <span class="fc" id="L49"> public DomainPermission(String actions) {</span> |
| <span class="fc" id="L50"> domain = getDomain(getClass());</span> |
| <span class="fc" id="L51"> this.actions = StringUtils.splitToSet(actions, SUBPART_DIVIDER_TOKEN);</span> |
| <span class="fc" id="L52"> encodeParts(domain, actions, null);</span> |
| <span class="fc" id="L53"> }</span> |
| |
| <span class="fc" id="L55"> public DomainPermission(String actions, String targets) {</span> |
| <span class="fc" id="L56"> this.domain = getDomain(getClass());</span> |
| <span class="fc" id="L57"> this.actions = StringUtils.splitToSet(actions, SUBPART_DIVIDER_TOKEN);</span> |
| <span class="fc" id="L58"> this.targets = StringUtils.splitToSet(targets, SUBPART_DIVIDER_TOKEN);</span> |
| <span class="fc" id="L59"> encodeParts(this.domain, actions, targets);</span> |
| <span class="fc" id="L60"> }</span> |
| |
| <span class="nc" id="L62"> protected DomainPermission(Set<String> actions, Set<String> targets) {</span> |
| <span class="nc" id="L63"> this.domain = getDomain(getClass());</span> |
| <span class="nc" id="L64"> setParts(domain, actions, targets);</span> |
| <span class="nc" id="L65"> }</span> |
| |
| private void encodeParts(String domain, String actions, String targets) { |
| <span class="pc bpc" id="L68" title="1 of 2 branches missed."> if (!StringUtils.hasText(domain)) {</span> |
| <span class="nc" id="L69"> throw new IllegalArgumentException("domain argument cannot be null or empty.");</span> |
| } |
| <span class="fc" id="L71"> StringBuilder sb = new StringBuilder(domain);</span> |
| |
| <span class="pc bpc" id="L73" title="1 of 2 branches missed."> if (!StringUtils.hasText(actions)) {</span> |
| <span class="nc bnc" id="L74" title="All 2 branches missed."> if (StringUtils.hasText(targets)) {</span> |
| <span class="nc" id="L75"> sb.append(PART_DIVIDER_TOKEN).append(WILDCARD_TOKEN);</span> |
| } |
| } else { |
| <span class="fc" id="L78"> sb.append(PART_DIVIDER_TOKEN).append(actions);</span> |
| } |
| <span class="fc bfc" id="L80" title="All 2 branches covered."> if (StringUtils.hasText(targets)) {</span> |
| <span class="fc" id="L81"> sb.append(PART_DIVIDER_TOKEN).append(targets);</span> |
| } |
| <span class="fc" id="L83"> setParts(sb.toString());</span> |
| <span class="fc" id="L84"> }</span> |
| |
| protected void setParts(String domain, Set<String> actions, Set<String> targets) { |
| <span class="nc" id="L87"> String actionsString = StringUtils.toDelimitedString(actions, SUBPART_DIVIDER_TOKEN);</span> |
| <span class="nc" id="L88"> String targetsString = StringUtils.toDelimitedString(targets, SUBPART_DIVIDER_TOKEN);</span> |
| <span class="nc" id="L89"> encodeParts(domain, actionsString, targetsString);</span> |
| <span class="nc" id="L90"> this.domain = domain;</span> |
| <span class="nc" id="L91"> this.actions = actions;</span> |
| <span class="nc" id="L92"> this.targets = targets;</span> |
| <span class="nc" id="L93"> }</span> |
| |
| protected String getDomain(Class<? extends DomainPermission> clazz) { |
| <span class="fc" id="L96"> String domain = clazz.getSimpleName().toLowerCase();</span> |
| //strip any trailing 'permission' text from the name (as all subclasses should have been named): |
| <span class="fc" id="L98"> int index = domain.lastIndexOf("permission");</span> |
| <span class="pc bpc" id="L99" title="1 of 2 branches missed."> if (index != -1) {</span> |
| <span class="fc" id="L100"> domain = domain.substring(0, index);</span> |
| } |
| <span class="fc" id="L102"> return domain;</span> |
| } |
| |
| public String getDomain() { |
| <span class="fc" id="L106"> return domain;</span> |
| } |
| |
| protected void setDomain(String domain) { |
| <span class="nc bnc" id="L110" title="All 4 branches missed."> if (this.domain != null && this.domain.equals(domain)) {</span> |
| <span class="nc" id="L111"> return;</span> |
| } |
| <span class="nc" id="L113"> this.domain = domain;</span> |
| <span class="nc" id="L114"> setParts(domain, actions, targets);</span> |
| <span class="nc" id="L115"> }</span> |
| |
| public Set<String> getActions() { |
| <span class="fc" id="L118"> return actions;</span> |
| } |
| |
| protected void setActions(Set<String> actions) { |
| <span class="nc bnc" id="L122" title="All 4 branches missed."> if (this.actions != null && this.actions.equals(actions)) {</span> |
| <span class="nc" id="L123"> return;</span> |
| } |
| <span class="nc" id="L125"> this.actions = actions;</span> |
| <span class="nc" id="L126"> setParts(domain, actions, targets);</span> |
| <span class="nc" id="L127"> }</span> |
| |
| public Set<String> getTargets() { |
| <span class="fc" id="L130"> return targets;</span> |
| } |
| |
| protected void setTargets(Set<String> targets) { |
| <span class="nc bnc" id="L134" title="All 4 branches missed."> if (this.targets != null && this.targets.equals(targets)) {</span> |
| <span class="nc" id="L135"> return;</span> |
| } |
| <span class="nc" id="L137"> this.targets = targets;</span> |
| <span class="nc" id="L138"> setParts(domain, actions, targets);</span> |
| <span class="nc" id="L139"> }</span> |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html> |