GUACAMOLE-5: Add SharingProfiles to REST API.

This commit is contained in:
Michael Jumper
2016-07-15 18:10:05 -07:00
parent 2131abb480
commit c231f4eb57
13 changed files with 789 additions and 7 deletions

View File

@@ -39,6 +39,7 @@ import org.apache.guacamole.rest.connectiongroup.ConnectionGroupModule;
import org.apache.guacamole.rest.language.LanguageRESTService; import org.apache.guacamole.rest.language.LanguageRESTService;
import org.apache.guacamole.rest.patch.PatchRESTService; import org.apache.guacamole.rest.patch.PatchRESTService;
import org.apache.guacamole.rest.session.SessionResourceFactory; import org.apache.guacamole.rest.session.SessionResourceFactory;
import org.apache.guacamole.rest.sharingprofile.SharingProfileModule;
import org.apache.guacamole.rest.user.UserModule; import org.apache.guacamole.rest.user.UserModule;
/** /**
@@ -96,6 +97,7 @@ public class RESTServiceModule extends ServletModule {
install(new ActiveConnectionModule()); install(new ActiveConnectionModule());
install(new ConnectionModule()); install(new ConnectionModule());
install(new ConnectionGroupModule()); install(new ConnectionGroupModule());
install(new SharingProfileModule());
install(new UserModule()); install(new UserModule());
// Set up the servlet and JSON mappings // Set up the servlet and JSON mappings

View File

@@ -39,6 +39,16 @@ public class APIConnectionRecord {
*/ */
private final String connectionName; private final String connectionName;
/**
* The identifier of the sharing profile associated with this record.
*/
private final String sharingProfileIdentifier;
/**
* The identifier of the sharing profile associated with this record.
*/
private final String sharingProfileName;
/** /**
* The date and time the connection began. * The date and time the connection began.
*/ */
@@ -73,13 +83,15 @@ public class APIConnectionRecord {
* The record to copy data from. * The record to copy data from.
*/ */
public APIConnectionRecord(ConnectionRecord record) { public APIConnectionRecord(ConnectionRecord record) {
this.connectionIdentifier = record.getConnectionIdentifier(); this.connectionIdentifier = record.getConnectionIdentifier();
this.connectionName = record.getConnectionName(); this.connectionName = record.getConnectionName();
this.startDate = record.getStartDate(); this.sharingProfileIdentifier = record.getSharingProfileIdentifier();
this.endDate = record.getEndDate(); this.sharingProfileName = record.getSharingProfileName();
this.remoteHost = record.getRemoteHost(); this.startDate = record.getStartDate();
this.username = record.getUsername(); this.endDate = record.getEndDate();
this.active = record.isActive(); this.remoteHost = record.getRemoteHost();
this.username = record.getUsername();
this.active = record.isActive();
} }
/** /**
@@ -103,6 +115,32 @@ public class APIConnectionRecord {
return connectionName; return connectionName;
} }
/**
* Returns the identifier of the sharing profile associated with this
* record. If the connection was not being used via a sharing profile, this
* will be null.
*
* @return
* The identifier of the sharing profile associated with this record,
* or null if no sharing profile was used.
*/
public String getSharingProfileIdentifier() {
return sharingProfileIdentifier;
}
/**
* Returns the name of the sharing profile associated with this record. If
* the connection was not being used via a sharing profile, this will be
* null.
*
* @return
* The name of the sharing profile associated with this record, or null
* if no sharing profile was used.
*/
public String getSharingProfileName() {
return sharingProfileName;
}
/** /**
* Returns the date and time the connection began. * Returns the date and time the connection began.
* *

View File

@@ -53,6 +53,12 @@ public class APIPermissionSet {
private Map<String, Set<ObjectPermission.Type>> connectionGroupPermissions = private Map<String, Set<ObjectPermission.Type>> connectionGroupPermissions =
new HashMap<String, Set<ObjectPermission.Type>>(); new HashMap<String, Set<ObjectPermission.Type>>();
/**
* Map of sharing profile ID to the set of granted permissions.
*/
private Map<String, Set<ObjectPermission.Type>> sharingProfilePermissions =
new HashMap<String, Set<ObjectPermission.Type>>();
/** /**
* Map of active connection ID to the set of granted permissions. * Map of active connection ID to the set of granted permissions.
*/ */
@@ -155,6 +161,7 @@ public class APIPermissionSet {
addSystemPermissions(systemPermissions, user.getSystemPermissions()); addSystemPermissions(systemPermissions, user.getSystemPermissions());
addObjectPermissions(connectionPermissions, user.getConnectionPermissions()); addObjectPermissions(connectionPermissions, user.getConnectionPermissions());
addObjectPermissions(connectionGroupPermissions, user.getConnectionGroupPermissions()); addObjectPermissions(connectionGroupPermissions, user.getConnectionGroupPermissions());
addObjectPermissions(sharingProfilePermissions, user.getSharingProfilePermissions());
addObjectPermissions(activeConnectionPermissions, user.getActiveConnectionPermissions()); addObjectPermissions(activeConnectionPermissions, user.getActiveConnectionPermissions());
addObjectPermissions(userPermissions, user.getUserPermissions()); addObjectPermissions(userPermissions, user.getUserPermissions());
@@ -190,6 +197,21 @@ public class APIPermissionSet {
return connectionGroupPermissions; return connectionGroupPermissions;
} }
/**
* Returns a map of sharing profile identifiers to the set of permissions
* granted for that sharing profile. If no permissions are granted to a
* particular sharing profile, its identifier will not be present as a key
* in the map. This map is mutable, and changes to this map will affect the
* permission set directly.
*
* @return
* A map of sharing profile identifiers to the set of permissions
* granted for that sharing profile.
*/
public Map<String, Set<ObjectPermission.Type>> getSharingProfilePermissions() {
return sharingProfilePermissions;
}
/** /**
* Returns a map of active connection IDs to the set of permissions granted * Returns a map of active connection IDs to the set of permissions granted
* for that active connection. If no permissions are granted to a particular * for that active connection. If no permissions are granted to a particular
@@ -257,6 +279,19 @@ public class APIPermissionSet {
this.connectionGroupPermissions = connectionGroupPermissions; this.connectionGroupPermissions = connectionGroupPermissions;
} }
/**
* Replaces the current map of sharing profile permissions with the given
* map, which must map each sharing profile identifier to its corresponding
* set of granted permissions. If a sharing profile has no permissions, its
* identifier must not be present as a key in the map.
*
* @param sharingProfilePermissions
* The map which must replace the currently-stored map of permissions.
*/
public void setSharingProfilePermissions(Map<String, Set<ObjectPermission.Type>> sharingProfilePermissions) {
this.sharingProfilePermissions = sharingProfilePermissions;
}
/** /**
* Replaces the current map of active connection permissions with the give * Replaces the current map of active connection permissions with the give
* map, which must map active connection ID to its corresponding set of * map, which must map active connection ID to its corresponding set of

View File

@@ -55,6 +55,12 @@ public class PermissionSetResource {
*/ */
private static final String CONNECTION_GROUP_PERMISSION_PATCH_PATH_PREFIX = "/connectionGroupPermissions/"; private static final String CONNECTION_GROUP_PERMISSION_PATCH_PATH_PREFIX = "/connectionGroupPermissions/";
/**
* The prefix of any path within an operation of a JSON patch which
* modifies the permissions of a user regarding a specific sharing profile.
*/
private static final String SHARING_PROFILE_PERMISSION_PATCH_PATH_PREFIX = "/sharingProfilePermissions/";
/** /**
* The prefix of any path within an operation of a JSON patch which * The prefix of any path within an operation of a JSON patch which
* modifies the permissions of a user regarding a specific active connection. * modifies the permissions of a user regarding a specific active connection.
@@ -170,6 +176,7 @@ public class PermissionSetResource {
// Permission patches for all types of permissions // Permission patches for all types of permissions
PermissionSetPatch<ObjectPermission> connectionPermissionPatch = new PermissionSetPatch<ObjectPermission>(); PermissionSetPatch<ObjectPermission> connectionPermissionPatch = new PermissionSetPatch<ObjectPermission>();
PermissionSetPatch<ObjectPermission> connectionGroupPermissionPatch = new PermissionSetPatch<ObjectPermission>(); PermissionSetPatch<ObjectPermission> connectionGroupPermissionPatch = new PermissionSetPatch<ObjectPermission>();
PermissionSetPatch<ObjectPermission> sharingProfilePermissionPatch = new PermissionSetPatch<ObjectPermission>();
PermissionSetPatch<ObjectPermission> activeConnectionPermissionPatch = new PermissionSetPatch<ObjectPermission>(); PermissionSetPatch<ObjectPermission> activeConnectionPermissionPatch = new PermissionSetPatch<ObjectPermission>();
PermissionSetPatch<ObjectPermission> userPermissionPatch = new PermissionSetPatch<ObjectPermission>(); PermissionSetPatch<ObjectPermission> userPermissionPatch = new PermissionSetPatch<ObjectPermission>();
PermissionSetPatch<SystemPermission> systemPermissionPatch = new PermissionSetPatch<SystemPermission>(); PermissionSetPatch<SystemPermission> systemPermissionPatch = new PermissionSetPatch<SystemPermission>();
@@ -205,6 +212,19 @@ public class PermissionSetResource {
} }
// Create sharing profile permission if path has sharing profileprefix
else if (path.startsWith(SHARING_PROFILE_PERMISSION_PATCH_PATH_PREFIX)) {
// Get identifier and type from patch operation
String identifier = path.substring(SHARING_PROFILE_PERMISSION_PATCH_PATH_PREFIX.length());
ObjectPermission.Type type = ObjectPermission.Type.valueOf(patch.getValue());
// Create and update corresponding permission
ObjectPermission permission = new ObjectPermission(type, identifier);
updatePermissionSet(patch.getOp(), sharingProfilePermissionPatch, permission);
}
// Create active connection permission if path has active connection prefix // Create active connection permission if path has active connection prefix
else if (path.startsWith(ACTIVE_CONNECTION_PERMISSION_PATCH_PATH_PREFIX)) { else if (path.startsWith(ACTIVE_CONNECTION_PERMISSION_PATCH_PATH_PREFIX)) {
@@ -252,6 +272,7 @@ public class PermissionSetResource {
// Save the permission changes // Save the permission changes
connectionPermissionPatch.apply(user.getConnectionPermissions()); connectionPermissionPatch.apply(user.getConnectionPermissions());
connectionGroupPermissionPatch.apply(user.getConnectionGroupPermissions()); connectionGroupPermissionPatch.apply(user.getConnectionGroupPermissions());
sharingProfilePermissionPatch.apply(user.getSharingProfilePermissions());
activeConnectionPermissionPatch.apply(user.getActiveConnectionPermissions()); activeConnectionPermissionPatch.apply(user.getActiveConnectionPermissions());
userPermissionPatch.apply(user.getUserPermissions()); userPermissionPatch.apply(user.getUserPermissions());
systemPermissionPatch.apply(user.getSystemPermissions()); systemPermissionPatch.apply(user.getSystemPermissions());

View File

@@ -32,6 +32,7 @@ import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.ActiveConnection; import org.apache.guacamole.net.auth.ActiveConnection;
import org.apache.guacamole.net.auth.Connection; import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionGroup; import org.apache.guacamole.net.auth.ConnectionGroup;
import org.apache.guacamole.net.auth.SharingProfile;
import org.apache.guacamole.net.auth.User; import org.apache.guacamole.net.auth.User;
import org.apache.guacamole.net.auth.UserContext; import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.rest.activeconnection.APIActiveConnection; import org.apache.guacamole.rest.activeconnection.APIActiveConnection;
@@ -39,6 +40,7 @@ import org.apache.guacamole.rest.connection.APIConnection;
import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup; import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup;
import org.apache.guacamole.rest.history.HistoryResource; import org.apache.guacamole.rest.history.HistoryResource;
import org.apache.guacamole.rest.schema.SchemaResource; import org.apache.guacamole.rest.schema.SchemaResource;
import org.apache.guacamole.rest.sharingprofile.APISharingProfile;
import org.apache.guacamole.rest.user.APIUser; import org.apache.guacamole.rest.user.APIUser;
/** /**
@@ -79,6 +81,14 @@ public class UserContextResource {
private DirectoryResourceFactory<ConnectionGroup, APIConnectionGroup> private DirectoryResourceFactory<ConnectionGroup, APIConnectionGroup>
connectionGroupDirectoryResourceFactory; connectionGroupDirectoryResourceFactory;
/**
* Factory for creating DirectoryResources which expose a given
* SharingProfile Directory.
*/
@Inject
private DirectoryResourceFactory<SharingProfile, APISharingProfile>
sharingProfileDirectoryResourceFactory;
/** /**
* Factory for creating DirectoryResources which expose a given * Factory for creating DirectoryResources which expose a given
* User Directory. * User Directory.
@@ -153,6 +163,24 @@ public class UserContextResource {
userContext.getConnectionGroupDirectory()); userContext.getConnectionGroupDirectory());
} }
/**
* Returns a new resource which represents the SharingProfile Directory
* contained within the UserContext exposed by this UserContextResource.
*
* @return
* A new resource which represents the SharingProfile Directory
* contained within the UserContext exposed by this UserContextResource.
*
* @throws GuacamoleException
* If an error occurs while retrieving the SharingProfile Directory.
*/
@Path("sharingProfiles")
public DirectoryResource<SharingProfile, APISharingProfile>
getSharingProfileDirectoryResource() throws GuacamoleException {
return sharingProfileDirectoryResourceFactory.create(userContext,
userContext.getSharingProfileDirectory());
}
/** /**
* Returns a new resource which represents the User Directory contained * Returns a new resource which represents the User Directory contained
* within the UserContext exposed by this UserContextResource. * within the UserContext exposed by this UserContextResource.

View File

@@ -0,0 +1,207 @@
/*
* 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.guacamole.rest.sharingprofile;
import java.util.Map;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.apache.guacamole.net.auth.SharingProfile;
/**
* The external representation used by the REST API for sharing profiles.
*
* @author Michael Jumper
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class APISharingProfile {
/**
* The human-readable name of this sharing profile.
*/
private String name;
/**
* The unique string which identifies this sharing profile within its
* containing directory.
*/
private String identifier;
/**
* The identifier of the primary connection that this sharing profile
* can be used to share.
*/
private String primaryConnectionIdentifier;
/**
* Map of all associated connection parameter values which apply when the
* sharing profile is used, indexed by parameter name.
*/
private Map<String, String> parameters;
/**
* Map of all associated attributes by attribute identifier.
*/
private Map<String, String> attributes;
/**
* Creates an empty, uninitialized APISharingProfile. The properties of the
* created APISharingProfile will need to be set individually as necessary
* via their corresponding setters.
*/
public APISharingProfile() {}
/**
* Creates a new APISharingProfile with its data populated from that of an
* existing SharingProfile. As the connection parameters of the
* SharingProfile are potentially sensitive, they will not be included in
* the new APISharingProfile.
*
* @param sharingProfile
* The sharing profile to use to populate the data of the new
* APISharingProfile.
*/
public APISharingProfile(SharingProfile sharingProfile) {
// Set main information
this.name = sharingProfile.getName();
this.identifier = sharingProfile.getIdentifier();
this.primaryConnectionIdentifier = sharingProfile.getPrimaryConnectionIdentifier();
// Associate any attributes
this.attributes = sharingProfile.getAttributes();
}
/**
* Returns the human-readable name of this sharing profile.
*
* @return
* The human-readable name of this sharing profile.
*/
public String getName() {
return name;
}
/**
* Set the human-readable name of this sharing profile.
*
* @param name
* The human-readable name of this sharing profile.
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the unique string which identifies this sharing profile within
* its containing directory.
*
* @return
* The unique string which identifies this sharing profile within its
* containing directory.
*/
public String getIdentifier() {
return identifier;
}
/**
* Sets the unique string which identifies this sharing profile within
* its containing directory.
*
* @param identifier
* The unique string which identifies this sharing profile within its
* containing directory.
*/
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
/**
* Returns the identifier of the primary connection that this sharing
* profile can be used to share.
*
* @return
* The identifier of the primary connection that this sharing profile
* can be used to share.
*/
public String getPrimaryConnectionIdentifier() {
return primaryConnectionIdentifier;
}
/**
* Sets the identifier of the primary connection that this sharing profile
* can be used to share.
*
* @param primaryConnectionIdentifier
* The identifier of the primary connection that this sharing profile
* can be used to share.
*/
public void setPrimaryConnectionIdentifier(String primaryConnectionIdentifier) {
this.primaryConnectionIdentifier = primaryConnectionIdentifier;
}
/**
* Returns a map of all associated connection parameter values which apply
* when the sharing profile is used, indexed by parameter name.
*
* @return
* A map of all associated connection parameter values which apply when
* the sharing profile is used, indexed by parameter name.
*/
public Map<String, String> getParameters() {
return parameters;
}
/**
* Sets the map of all associated connection parameter values which apply
* when the sharing profile is used, indexed by parameter name.
*
* @param parameters
* The map of all associated connection parameter values which apply
* when the sharing profile is used, indexed by parameter name.
*/
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}
/**
* Returns a map of all attributes associated with this sharing profile.
* Each entry key is the attribute identifier, while each value is the
* attribute value itself.
*
* @return
* The attribute map for this sharing profile.
*/
public Map<String, String> getAttributes() {
return attributes;
}
/**
* Sets the map of all attributes associated with this sharing profile. Each
* entry key is the attribute identifier, while each value is the attribute
* value itself.
*
* @param attributes
* The attribute map for this sharing profile.
*/
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
}

View File

@@ -0,0 +1,101 @@
/*
* 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.guacamole.rest.sharingprofile;
import java.util.Map;
import org.apache.guacamole.net.auth.SharingProfile;
/**
* Wrapper for APISharingProfile which provides a SharingProfile interface.
* Changes to the underlying APISharingProfile are reflected immediately in the
* values exposed by the SharingProfile interface, and changes made through the
* SharingProfile interface immediately affect the underlying APISharingProfile.
*
* @author Michael Jumper
*/
public class APISharingProfileWrapper implements SharingProfile {
/**
* The wrapped APISharingProfile.
*/
private final APISharingProfile apiSharingProfile;
/**
* Creates a new APISharingProfileWrapper which is backed by the given
* APISharingProfile.
*
* @param apiSharingProfile
* The APISharingProfile to wrap.
*/
public APISharingProfileWrapper(APISharingProfile apiSharingProfile) {
this.apiSharingProfile = apiSharingProfile;
}
@Override
public String getName() {
return apiSharingProfile.getName();
}
@Override
public void setName(String name) {
apiSharingProfile.setName(name);
}
@Override
public String getIdentifier() {
return apiSharingProfile.getIdentifier();
}
@Override
public void setIdentifier(String identifier) {
apiSharingProfile.setIdentifier(identifier);
}
@Override
public String getPrimaryConnectionIdentifier() {
return apiSharingProfile.getPrimaryConnectionIdentifier();
}
@Override
public void setPrimaryConnectionIdentifier(String primaryConnectionIdentifier) {
apiSharingProfile.setPrimaryConnectionIdentifier(primaryConnectionIdentifier);
}
@Override
public Map<String, String> getParameters() {
return apiSharingProfile.getParameters();
}
@Override
public void setParameters(Map<String, String> parameters) {
apiSharingProfile.setParameters(parameters);
}
@Override
public Map<String, String> getAttributes() {
return apiSharingProfile.getAttributes();
}
@Override
public void setAttributes(Map<String, String> attributes) {
apiSharingProfile.setAttributes(attributes);
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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.guacamole.rest.sharingprofile;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.SharingProfile;
import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.rest.directory.DirectoryObjectResourceFactory;
import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
import org.apache.guacamole.rest.directory.DirectoryResource;
/**
* A REST resource which abstracts the operations available on a Directory of
* SharingProfiles.
*
* @author Michael Jumper
*/
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SharingProfileDirectoryResource
extends DirectoryResource<SharingProfile, APISharingProfile> {
/**
* Creates a new SharingProfileDirectoryResource which exposes the
* operations and subresources available for the given SharingProfile
* Directory.
*
* @param userContext
* The UserContext associated with the given Directory.
*
* @param directory
* The Directory being exposed.
*
* @param translator
* A DirectoryObjectTranslator implementation which handles
* SharingProfiles.
*
* @param resourceFactory
* A factory which can be used to create instances of resources
* representing SharingProfiles.
*/
@AssistedInject
public SharingProfileDirectoryResource(@Assisted UserContext userContext,
@Assisted Directory<SharingProfile> directory,
DirectoryObjectTranslator<SharingProfile, APISharingProfile> translator,
DirectoryObjectResourceFactory<SharingProfile, APISharingProfile> resourceFactory) {
super(userContext, directory, translator, resourceFactory);
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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.guacamole.rest.sharingprofile;
import com.google.inject.AbstractModule;
import org.apache.guacamole.rest.directory.DirectoryObjectResourceFactory;
import org.apache.guacamole.rest.directory.DirectoryObjectResource;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import org.apache.guacamole.net.auth.SharingProfile;
import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
import org.apache.guacamole.rest.directory.DirectoryResource;
import org.apache.guacamole.rest.directory.DirectoryResourceFactory;
/**
* Guice Module which configures injections required for handling SharingProfile
* resources via the REST API.
*
* @author Michael Jumper
*/
public class SharingProfileModule extends AbstractModule {
@Override
protected void configure() {
// Create the required DirectoryResourceFactory implementation
install(new FactoryModuleBuilder()
.implement(
new TypeLiteral<DirectoryResource<SharingProfile, APISharingProfile>>() {},
SharingProfileDirectoryResource.class
)
.build(new TypeLiteral<DirectoryResourceFactory<SharingProfile, APISharingProfile>>() {}));
// Create the required DirectoryObjectResourceFactory implementation
install(new FactoryModuleBuilder()
.implement(
new TypeLiteral<DirectoryObjectResource<SharingProfile, APISharingProfile>>() {},
SharingProfileResource.class
)
.build(new TypeLiteral<DirectoryObjectResourceFactory<SharingProfile, APISharingProfile>>() {}));
// Bind translator for converting between SharingProfile and APISharingProfile
bind(new TypeLiteral<DirectoryObjectTranslator<SharingProfile, APISharingProfile>>() {})
.to(SharingProfileObjectTranslator.class);
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.guacamole.rest.sharingprofile;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.SharingProfile;
import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
/**
* Translator which converts between SharingProfile objects and
* APISharingProfile objects.
*
* @author Michael Jumper
*/
public class SharingProfileObjectTranslator
implements DirectoryObjectTranslator<SharingProfile, APISharingProfile> {
@Override
public APISharingProfile toExternalObject(SharingProfile object)
throws GuacamoleException {
return new APISharingProfile(object);
}
@Override
public SharingProfile toInternalObject(APISharingProfile object) {
return new APISharingProfileWrapper(object);
}
@Override
public void applyExternalChanges(SharingProfile existingObject,
APISharingProfile object) {
// Update the sharing profile
existingObject.setPrimaryConnectionIdentifier(object.getPrimaryConnectionIdentifier());
existingObject.setName(object.getName());
existingObject.setParameters(object.getParameters());
existingObject.setAttributes(object.getAttributes());
}
}

View File

@@ -0,0 +1,125 @@
/*
* 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.guacamole.rest.sharingprofile;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleSecurityException;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.SharingProfile;
import org.apache.guacamole.net.auth.User;
import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.net.auth.permission.ObjectPermission;
import org.apache.guacamole.net.auth.permission.ObjectPermissionSet;
import org.apache.guacamole.net.auth.permission.SystemPermission;
import org.apache.guacamole.net.auth.permission.SystemPermissionSet;
import org.apache.guacamole.rest.directory.DirectoryObjectResource;
import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
/**
* A REST resource which abstracts the operations available on an existing
* SharingProfile.
*
* @author Michael Jumper
*/
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class SharingProfileResource
extends DirectoryObjectResource<SharingProfile, APISharingProfile> {
/**
* The UserContext associated with the Directory which contains the
* SharingProfile exposed by this resource.
*/
private final UserContext userContext;
/**
* The SharingProfile object represented by this SharingProfileResource.
*/
private final SharingProfile sharingProfile;
/**
* Creates a new SharingProfileResource which exposes the operations and
* subresources available for the given SharingProfile.
*
* @param userContext
* The UserContext associated with the given Directory.
*
* @param directory
* The Directory which contains the given SharingProfile.
*
* @param sharingProfile
* The SharingProfile that this SharingProfileResource should represent.
*
* @param translator
* A DirectoryObjectTranslator implementation which handles the type of
* object given.
*/
@AssistedInject
public SharingProfileResource(@Assisted UserContext userContext,
@Assisted Directory<SharingProfile> directory,
@Assisted SharingProfile sharingProfile,
DirectoryObjectTranslator<SharingProfile, APISharingProfile> translator) {
super(directory, sharingProfile, translator);
this.userContext = userContext;
this.sharingProfile = sharingProfile;
}
/**
* Retrieves the connection parameters associated with the SharingProfile
* exposed by this SharingProfile resource.
*
* @return
* A map of parameter name/value pairs.
*
* @throws GuacamoleException
* If an error occurs while retrieving the connection parameters of the
* SharingProfile.
*/
@GET
@Path("parameters")
public Map<String, String> getParameters()
throws GuacamoleException {
User self = userContext.self();
// Retrieve permission sets
SystemPermissionSet systemPermissions = self.getSystemPermissions();
ObjectPermissionSet sharingProfilePermissions = self.getSharingProfilePermissions();
// Deny access if adminstrative or update permission is missing
String identifier = sharingProfile.getIdentifier();
if (!systemPermissions.hasPermission(SystemPermission.Type.ADMINISTER)
&& !sharingProfilePermissions.hasPermission(ObjectPermission.Type.UPDATE, identifier))
throw new GuacamoleSecurityException("Permission to read sharing profile parameters denied.");
// Return parameter map
return sharingProfile.getParameters();
}
}

View File

@@ -0,0 +1,25 @@
/*
* 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.
*/
/**
* Classes related to retrieving or manipulating sharing profiles using the
* Guacamole REST API.
*/
package org.apache.guacamole.rest.sharingprofile;

View File

@@ -97,6 +97,11 @@ public class APIUserWrapper implements User {
throw new GuacamoleUnsupportedException("APIUserWrapper does not provide permission access."); throw new GuacamoleUnsupportedException("APIUserWrapper does not provide permission access.");
} }
@Override
public ObjectPermissionSet getSharingProfilePermissions() throws GuacamoleException {
throw new GuacamoleUnsupportedException("APIUserWrapper does not provide permission access.");
}
@Override @Override
public ObjectPermissionSet getUserPermissions() public ObjectPermissionSet getUserPermissions()
throws GuacamoleException { throws GuacamoleException {