File Source: AuthoritiesPopulator.java

         /* 
    P/P   *  Method: org.apache.roller.weblogger.ui.core.security.AuthoritiesPopulator__static_init
          */
     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  package org.apache.roller.weblogger.ui.core.security;
    19  
    20  import java.util.Iterator;
    21  
    22  import org.acegisecurity.GrantedAuthority;
    23  import org.acegisecurity.GrantedAuthorityImpl;
    24  import org.acegisecurity.ldap.LdapDataAccessException;
    25  import org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator;
    26  import org.acegisecurity.userdetails.UsernameNotFoundException;
    27  import org.acegisecurity.userdetails.ldap.LdapUserDetails;
    28  import org.apache.roller.weblogger.WebloggerException;
    29  import org.apache.roller.weblogger.business.UserManager;
    30  import org.apache.roller.weblogger.business.Weblogger;
    31  import org.apache.roller.weblogger.business.WebloggerFactory;
    32  import org.apache.roller.weblogger.pojos.User;
    33  import org.apache.roller.weblogger.pojos.UserRole;
    34  import org.springframework.util.Assert;
    35  
    36  
    37  /**
    38   * @author Elias Torres (<a href="mailto:eliast@us.ibm.com">eliast@us.ibm.com</a>)
    39   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.core.security.AuthoritiesPopulator()
          * 
          *  Postconditions:
          *    this.defaultRole == null
          */
    40  public class AuthoritiesPopulator implements LdapAuthoritiesPopulator {
    41  
    42      /** A default role which will be assigned to all authenticated users if set */
    43      private GrantedAuthority defaultRole = null;
    44  
    45      
    46      /* (non-Javadoc)
    47       * @see org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator#getGrantedAuthorities(org.acegisecurity.userdetails.ldap.LdapUserDetails)
    48       */
    49      public GrantedAuthority[] getGrantedAuthorities(LdapUserDetails userDetails) throws LdapDataAccessException {
    50  
    51  
                 /* 
    P/P           *  Method: GrantedAuthority[] getGrantedAuthorities(LdapUserDetails)
                  * 
                  *  Preconditions:
                  *    init'ed(this.defaultRole)
                  *    userDetails != null
                  * 
                  *  Presumptions:
                  *    init'ed(java.lang.Boolean.TRUE)
                  *    java.util.Iterator:next(...)@70 != null
                  *    java.util.Set:size(...)@65 in 1..232-2
                  *    org.apache.roller.weblogger.business.UserManager:getUserByUserName(...)@56 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@55 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    return_value == &new GrantedAuthorityImpl[](getGrantedAuthorities#4)
                  *    init'ed(new GrantedAuthorityImpl(getGrantedAuthorities#5) num objects)
                  *    new GrantedAuthorityImpl[](getGrantedAuthorities#4) num objects == 1
                  *    (soft) new GrantedAuthorityImpl[](getGrantedAuthorities#4).length in 1..232-1
                  *    new GrantedAuthorityImpl[](getGrantedAuthorities#4)[...] != null
                  * 
                  *  Test Vectors:
                  *    this.defaultRole: Addr_Set{null}, Inverse{null}
                  *    java.util.Iterator:hasNext(...)@69: {0}, {1}
                  */
    52          User userData = null;
    53          try {
    54              Weblogger roller = WebloggerFactory.getWeblogger();
    55              UserManager umgr = roller.getUserManager();
    56              userData = umgr.getUserByUserName(userDetails.getUsername(), Boolean.TRUE);
    57          } catch (WebloggerException ex) {
    58              throw new LdapDataAccessException("ERROR in user lookup", ex);
    59          }
    60  
    61          if (userData == null) {
    62              throw new LdapDataAccessException("ERROR no user: " + userDetails.getUsername());
    63          }
    64  
    65          int roleCount = userData.getRoles().size();
    66          if (defaultRole != null) roleCount++;
    67          GrantedAuthority[] authorities = new GrantedAuthorityImpl[roleCount];
    68          int i = 0;
    69          for (Iterator it = userData.getRoles().iterator(); it.hasNext();) {
    70              UserRole role = (UserRole) it.next();
+   71              authorities[i++] = new GrantedAuthorityImpl(role.getRole());
    72          }
    73          
    74          if (defaultRole != null) {
    75              authorities[roleCount-1] = defaultRole;
    76          }
    77  
    78          if (authorities.length == 0) {
    79              throw new UsernameNotFoundException("User has no GrantedAuthority");
    80          }
    81  
    82          return authorities;
    83      }
    84  
    85      /**
    86       * The default role which will be assigned to all users.
    87       *
    88       * @param defaultRole the role name, including any desired prefix.
    89       */
    90      public void setDefaultRole(String defaultRole) {
                 /* 
    P/P           *  Method: void setDefaultRole(String)
                  * 
                  *  Postconditions:
                  *    this.defaultRole == &new GrantedAuthorityImpl(setDefaultRole#1)
                  *    new GrantedAuthorityImpl(setDefaultRole#1) num objects == 1
                  */
    91          Assert.notNull(defaultRole, "The defaultRole property cannot be set to null");
    92          this.defaultRole = new GrantedAuthorityImpl(defaultRole);
    93      }
    94  }








SofCheck Inspector Build Version : 2.18479
AuthoritiesPopulator.java 2009-Jan-02 14:25:22
AuthoritiesPopulator.class 2009-Sep-04 03:12:44