View Javadoc
1   package fr.ifremer.dali.config;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.quadrige3.core.security.QuadrigeUserAuthority;
28  import fr.ifremer.quadrige3.core.security.SecurityContextHelper;
29  import org.nuiton.config.ConfigOptionDef;
30  
31  import java.util.List;
32  
33  /**
34   * <p>DaliSecuredConfigurationOption class.</p>
35   *
36   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
37   */
38  public class DaliSecuredConfigurationOption implements ConfigOptionDef {
39  
40      private final ConfigOptionDef delegate;
41      private final List<QuadrigeUserAuthority> roles;
42  
43      /**
44       * <p>Constructor for DaliSecuredConfigurationOption.</p>
45       *
46       * @param delegate a {@link org.nuiton.config.ConfigOptionDef} object.
47       * @param role a {@link QuadrigeUserAuthority} object.
48       */
49      public DaliSecuredConfigurationOption(ConfigOptionDef delegate, QuadrigeUserAuthority role) {
50          this.delegate = delegate;
51          this.roles = Lists.newArrayList(role);
52      }
53  
54      /**
55       * <p>Constructor for DaliSecuredConfigurationOption.</p>
56       *
57       * @param delegate a {@link org.nuiton.config.ConfigOptionDef} object.
58       * @param roles a {@link java.util.List} object.
59       */
60      public DaliSecuredConfigurationOption(ConfigOptionDef delegate, List<QuadrigeUserAuthority> roles) {
61          this.delegate = delegate;
62          this.roles = roles;
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public String getKey() {
68          return delegate.getKey();
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public Class<?> getType() {
74          return delegate.getType();
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public String getDescription() {
80          return delegate.getDescription();
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public String getDefaultValue() {
86          return delegate.getDefaultValue();
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public boolean isTransient() {
92          return delegate.isTransient();
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public boolean isFinal() {
98          return !SecurityContextHelper.hasAuthority(roles) || delegate.isFinal();
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public void setDefaultValue(String defaultValue) {
104         if (!SecurityContextHelper.hasAuthority(roles)) {
105             return;
106         }
107         delegate.setDefaultValue(defaultValue);
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public void setTransient(boolean isTransient) {
113         if (!SecurityContextHelper.hasAuthority(roles)) {
114             return;
115         }
116         delegate.setTransient(isTransient);
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public void setFinal(boolean isFinal) {
122         if (!SecurityContextHelper.hasAuthority(roles)) {
123             return;
124         }
125         delegate.setFinal(isFinal);
126     }
127 
128 }