~ubuntu-branches/ubuntu/maverick/eucalyptus/maverick

« back to all changes in this revision

Viewing changes to clc/modules/authentication/src/main/java/com/eucalyptus/auth/DatabaseWrappedGroup.java

  • Committer: Bazaar Package Importer
  • Author(s): Dave Walker (Daviey)
  • Date: 2010-07-21 17:27:10 UTC
  • mfrom: (1.1.38 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721172710-7xv07dmdqgivc3t9
Tags: 2.0~bzr1211-0ubuntu1
* New major upstream version merge, 2.0 (r1211).
* debian/patches/:
  - 01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated.
  - 02-Makefile.patch: Updated to reflect new code layout.
  - 07-local_support_euca_conf-in.patch: Updated to reflect new code layout.
  - 08-ubuntu-default-networking.patch: Refreshed.
  - 09-small-128-192MB.patch: Updated to point to new location.
  - 10-disable-iscsi.patch: Refreshed.
  - 11-state-cleanup-memleakfix.patch: Removed, fixed upstream.
  - 15-fix-default-ramdisk.patch: Updated to point to new location.
  - 16-kvm_libvirt_xml_default_use_kvm.patch: Updated to reflect changes.
  - 17-fix_walrus_OOM_errors.patch: Removed, fixed upstream.
  - 18-priv_security.patch: Updated to reflect upstream changes.
  - 20-brute-force-webui.patch: Updated to reflect upstream changes. 
  - 21-eucalyptus-1.7-with-gwt-1.6.4.patch: New patch, allows 
    eucalyptus-1.7 to be built against gwt 1.6.4. Based on patch courtesy 
    of Dmitrii Zagorodnov, upstream. (LP: #597330)
* debian/eucalyptus-java-common.links: 
  - Changed symlink for groovy, point to groovy.all.jar, making compatiable 
    with groovy versions >1.7. (LP: #595421)
  - Added ant.jar & jetty-rewrite-handler.jar as they are now required.
* debian/control
  - & debian/build-jars: Added libjavassist-java and libjetty-extra-java as 
    build dependencies.
  - Added libjetty-extra-java as a dependency of eucalyptus-java-common
* The binary resulting jar's have been renamed from eucalyptus-*-1.6.2.jar
  to eucalyptus-*-main.jar:    
  - debian/eucalyptus-cc.upstart
  - debian/eucalyptus-cloud.install
  - debian/eucalyptus-common.eucalyptus.upstart
  - debian/eucalyptus-java-common.install
  - debian/eucalyptus-network.upstart
  - debian/eucalyptus-sc.install
  - debian/eucalyptus-walrus.install
* debian/eucalyptus-java-common.install: New upstream jars that have been
  installed:
  - eucalyptus-db-hsqldb-ext-main.jar
  - eucalyptus-component-main.jar
* debian/control:
  - Updated Standards Version to 3.8.4 (no change)
  - Updated the upstream Homepage to: http://open.eucalyptus.com/
  - Changed Vcs-Bzr to reflect new location of Ubuntu hosted development branch.
  - Made the Build Dependency of groovy and the binary eucalyptus-java-common
    package depend on version >=1.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.eucalyptus.auth;
 
2
 
 
3
import java.security.Principal;
 
4
import java.util.Enumeration;
 
5
import java.util.List;
 
6
import org.apache.log4j.Logger;
 
7
import com.eucalyptus.auth.principal.Authorization;
 
8
import com.eucalyptus.auth.principal.BaseAuthorization;
 
9
import com.eucalyptus.auth.principal.Group;
 
10
import com.eucalyptus.auth.principal.User;
 
11
import com.eucalyptus.entities.EntityWrapper;
 
12
import com.eucalyptus.records.EventClass;
 
13
import com.eucalyptus.records.EventRecord;
 
14
import com.eucalyptus.records.EventType;
 
15
import com.eucalyptus.util.EucalyptusCloudException;
 
16
import com.eucalyptus.util.FinalReturn;
 
17
import com.eucalyptus.util.TransactionException;
 
18
import com.eucalyptus.util.Transactions;
 
19
import com.eucalyptus.util.Tx;
 
20
import com.google.common.collect.ImmutableList;
 
21
import com.google.common.collect.Iterators;
 
22
import com.google.common.collect.Lists;
 
23
 
 
24
public class DatabaseWrappedGroup implements Group {
 
25
  private static Logger LOG = Logger.getLogger( DatabaseWrappedGroup.class );
 
26
 
 
27
  public static Group newInstance( Group g ) {
 
28
    if( Groups.NAME_ALL.equals( g.getName( ) ) ) {
 
29
      return new AllGroup( g );
 
30
    } else {
 
31
      return new DatabaseWrappedGroup( g );
 
32
    }
 
33
  }
 
34
 
 
35
  private GroupEntity   searchGroup;
 
36
  private Group group;
 
37
  
 
38
  protected DatabaseWrappedGroup( Group group ) {
 
39
    this.searchGroup = new GroupEntity( group.getName( ) );
 
40
    this.group = group;
 
41
  }
 
42
  
 
43
  @Override
 
44
  public boolean addMember( Principal principal ) {
 
45
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
46
    try {
 
47
      UserEntity user = db.getUnique( new UserEntity( principal.getName( ) ) );
 
48
      GroupEntity g = db.recast( GroupEntity.class ).getUnique( this.searchGroup );
 
49
      if ( !g.isMember( user ) ) {
 
50
        g.addMember( user );
 
51
        db.commit( );
 
52
        EventRecord.here( Groups.class, EventClass.GROUP, EventType.GROUP_MEMBER_ADDED, this.getName( ), user.getName( ) ).info();
 
53
        return true;
 
54
      } else {
 
55
        db.rollback( );
 
56
        return false;
 
57
      }
 
58
    } catch ( EucalyptusCloudException e ) {
 
59
      LOG.debug( e, e );
 
60
      db.rollback( );
 
61
      return false;
 
62
    }
 
63
  }
 
64
  
 
65
  @Override
 
66
  public boolean isMember( Principal member ) {
 
67
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
68
    try {
 
69
      boolean ret = this.group.isMember( db.getUnique( new UserEntity( member.getName( ) ) ) );
 
70
      db.commit( );
 
71
      return ret;
 
72
    } catch ( EucalyptusCloudException e ) {
 
73
      LOG.debug( e, e );
 
74
      db.rollback( );
 
75
      return false;
 
76
    }
 
77
  }
 
78
  
 
79
  @Override
 
80
  public Enumeration<? extends Principal> members( ) {
 
81
    final List<User> userList = Lists.newArrayList( );
 
82
    try {
 
83
      Transactions.one( this.searchGroup, new Tx<GroupEntity>( ) {
 
84
        @Override
 
85
        public void fire( GroupEntity t ) throws Throwable {
 
86
          for( User user : t.getMembers( ) ) {
 
87
            try {
 
88
              userList.add( Users.lookupUser( user.getName( ) ) );
 
89
            } catch ( NoSuchUserException e ) {
 
90
              LOG.debug( e, e );
 
91
            }
 
92
          }
 
93
        }
 
94
      } );
 
95
    } catch ( TransactionException e1 ) {
 
96
      LOG.debug( e1, e1 );
 
97
    }
 
98
    return Iterators.asEnumeration( userList.iterator( ) );
 
99
  }
 
100
  
 
101
  @Override
 
102
  public boolean removeMember( Principal user ) {
 
103
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
104
    try {
 
105
      UserEntity userInfo = db.getUnique( new UserEntity( user.getName( ) ) );
 
106
      GroupEntity g = db.recast( GroupEntity.class ).getUnique( this.searchGroup );
 
107
      if ( g.isMember( userInfo ) ) {
 
108
        g.removeMember( userInfo );
 
109
        db.commit( );
 
110
        EventRecord.here( Groups.class, EventClass.GROUP, EventType.GROUP_MEMBER_REMOVED, this.getName( ), userInfo.getName( ) ).info();
 
111
        return true;
 
112
      } else {
 
113
        db.rollback( );
 
114
        return false;
 
115
      }
 
116
    } catch ( EucalyptusCloudException e ) {
 
117
      LOG.debug( e, e );
 
118
      db.rollback( );
 
119
      return false;
 
120
    }
 
121
  }
 
122
  
 
123
  @Override
 
124
  public String getName( ) {
 
125
    return this.group.getName( );
 
126
  }
 
127
  
 
128
  @Override
 
129
  public boolean equals( Object o ) {
 
130
    if ( this == o ) return true;
 
131
    if ( o instanceof GroupEntity ) {
 
132
      GroupEntity that = ( GroupEntity ) o;
 
133
      return this.group.equals( that );
 
134
    } else if ( o instanceof DatabaseWrappedGroup ) {
 
135
      DatabaseWrappedGroup that = ( DatabaseWrappedGroup ) o;
 
136
      return this.group.equals( that.group );
 
137
    } else {
 
138
      return false;
 
139
    }
 
140
  }
 
141
  
 
142
  @Override
 
143
  public boolean addAuthorization( final Authorization authorization ) {
 
144
    if ( authorization instanceof BaseAuthorization ) {
 
145
      BaseAuthorization auth = ( BaseAuthorization ) authorization;
 
146
      EntityWrapper<BaseAuthorization> db = EntityWrapper.get( BaseAuthorization.class );
 
147
      boolean ret = false;
 
148
      try {
 
149
        db.add( auth );
 
150
        GroupEntity g = db.recast( GroupEntity.class ).getUnique( searchGroup );
 
151
        ret = g.addAuthorization( auth );
 
152
        db.recast( GroupEntity.class ).merge( g );
 
153
        this.group = g;
 
154
        db.commit( );
 
155
        EventRecord.here( Groups.class, EventClass.GROUP, EventType.GROUP_AUTH_GRANTED, this.getName( ), auth.getDisplayName( ), auth.getValue( ) ).info();
 
156
      } catch ( Throwable e ) {
 
157
        ret = false;
 
158
        LOG.debug( e, e );
 
159
        db.rollback( );
 
160
      } 
 
161
      return ret;
 
162
    } else {
 
163
      throw new RuntimeException( "Authorizations must extend from BaseAuthorization, passed: " + authorization.getClass( ).getCanonicalName( ) );
 
164
    }
 
165
  }
 
166
  
 
167
  @Override
 
168
  public ImmutableList<Authorization> getAuthorizations( ) {
 
169
    final List<Authorization> auths = Lists.newArrayList( );
 
170
    try {
 
171
      Transactions.one( this.searchGroup, new Tx<GroupEntity>( ) {
 
172
        @Override
 
173
        public void fire( GroupEntity t ) throws Throwable {
 
174
          for( Authorization a : t.getAuthorizations( ) ) {
 
175
            auths.add( a );
 
176
          }
 
177
        }
 
178
      } );
 
179
    } catch ( TransactionException e ) {
 
180
      LOG.debug( e, e );
 
181
    }
 
182
    return ImmutableList.copyOf( auths );
 
183
  }
 
184
  
 
185
  @Override
 
186
  public ImmutableList<User> getMembers( ) {
 
187
    final List<User> users = Lists.newArrayList( );
 
188
    try {
 
189
      Transactions.one( this.searchGroup, new Tx<GroupEntity>( ) {
 
190
        @Override
 
191
        public void fire( GroupEntity t ) throws Throwable {
 
192
          users.addAll( t.getMembers( ) );
 
193
        }
 
194
      } );
 
195
    } catch ( TransactionException e ) {
 
196
      LOG.debug( e, e );
 
197
    }
 
198
    return ImmutableList.copyOf( users );
 
199
  }
 
200
  
 
201
  @Override
 
202
  public boolean removeAuthorization( final Authorization auth ) {
 
203
    final FinalReturn<Boolean> ret = FinalReturn.newInstance( );  
 
204
    try {
 
205
      Transactions.one( this.searchGroup, new Tx<GroupEntity>( ) {
 
206
        @Override
 
207
        public void fire( GroupEntity t ) throws Throwable {
 
208
           ret.set( t.removeAuthorization( auth ) );
 
209
           EventRecord.here( Groups.class, EventClass.GROUP, EventType.GROUP_AUTH_REVOKED, t.getName( ), auth.getDisplayName( ), auth.getValue( ) ).info();
 
210
        }
 
211
      } );
 
212
    } catch ( TransactionException e ) {
 
213
      LOG.debug( e, e );
 
214
    }
 
215
    return ret.get( );
 
216
  }
 
217
  
 
218
}