2020import java .util .List ;
2121import java .util .Map ;
2222
23+ import org .apache .cloudstack .acl .SecurityChecker ;
24+ import org .apache .cloudstack .api .response .GuestOSResponse ;
25+ import org .apache .cloudstack .api .response .SnapshotResponse ;
26+ import org .apache .cloudstack .api .response .TemplateResponse ;
27+ import org .apache .cloudstack .api .response .UserVmResponse ;
28+ import org .apache .cloudstack .api .response .VolumeResponse ;
29+ import org .apache .cloudstack .api .response .ProjectResponse ;
30+
2331import org .apache .log4j .Logger ;
2432
2533import org .apache .cloudstack .api .APICommand ;
3038import org .apache .cloudstack .api .Parameter ;
3139import org .apache .cloudstack .api .ResponseObject .ResponseView ;
3240import org .apache .cloudstack .api .ServerApiException ;
33- import org .apache .cloudstack .api .response .GuestOSResponse ;
34- import org .apache .cloudstack .api .response .SnapshotResponse ;
35- import org .apache .cloudstack .api .response .TemplateResponse ;
36- import org .apache .cloudstack .api .response .UserVmResponse ;
37- import org .apache .cloudstack .api .response .VolumeResponse ;
3841import org .apache .cloudstack .context .CallContext ;
3942
4043import com .cloud .event .EventTypes ;
@@ -124,6 +127,9 @@ public class CreateTemplateCmd extends BaseAsyncCreateCmd {
124127 description = "true if template contains XS/VMWare tools inorder to support dynamic scaling of VM cpu/memory" )
125128 protected Boolean isDynamicallyScalable ;
126129
130+ @ Parameter (name = ApiConstants .PROJECT_ID , type = CommandType .UUID , entityType = ProjectResponse .class , description = "create template for the project" )
131+ private Long projectId ;
132+
127133 // ///////////////////////////////////////////////////
128134 // ///////////////// Accessors ///////////////////////
129135 // ///////////////////////////////////////////////////
@@ -215,33 +221,39 @@ public long getEntityOwnerId() {
215221 if (volumeId != null ) {
216222 Volume volume = _entityMgr .findById (Volume .class , volumeId );
217223 if (volume != null ) {
218- accountId = volume . getAccountId ( );
224+ _accountService . checkAccess ( CallContext . current (). getCallingAccount (), SecurityChecker . AccessType . UseEntry , false , volume );
219225 } else {
220226 throw new InvalidParameterValueException ("Unable to find volume by id=" + volumeId );
221227 }
222228 } else {
223229 Snapshot snapshot = _entityMgr .findById (Snapshot .class , snapshotId );
224230 if (snapshot != null ) {
225- accountId = snapshot . getAccountId ( );
231+ _accountService . checkAccess ( CallContext . current (). getCallingAccount (), SecurityChecker . AccessType . UseEntry , false , snapshot );
226232 } else {
227233 throw new InvalidParameterValueException ("Unable to find snapshot by id=" + snapshotId );
228234 }
229235 }
230236
231- Account account = _accountService .getAccount (accountId );
232- //Can create templates for enabled projects/accounts only
233- if (account .getType () == Account .ACCOUNT_TYPE_PROJECT ) {
234- Project project = _projectService .findByProjectAccountId (accountId );
235- if (project .getState () != Project .State .Active ) {
236- PermissionDeniedException ex =
237- new PermissionDeniedException ("Can't add resources to the specified project id in state=" + project .getState () + " as it's no longer active" );
238- ex .addProxyObject (project .getUuid (), "projectId" );
237+ if (projectId != null ){
238+ final Project project = _projectService .getProject (projectId );
239+ if (project != null ) {
240+ if (project .getState () == Project .State .Active ) {
241+ Account projectAccount = _accountService .getAccount (project .getProjectAccountId ());
242+ _accountService .checkAccess (CallContext .current ().getCallingAccount (), SecurityChecker .AccessType .UseEntry , false , projectAccount );
243+ return project .getProjectAccountId ();
244+ } else {
245+ final PermissionDeniedException ex =
246+ new PermissionDeniedException ("Can't add resources to the project with specified projectId in state=" + project .getState () +
247+ " as it's no longer active" );
248+ ex .addProxyObject (project .getUuid (), "projectId" );
249+ throw ex ;
250+ }
251+ } else {
252+ throw new InvalidParameterValueException ("Unable to find project by id" );
239253 }
240- } else if (account .getState () == Account .State .disabled ) {
241- throw new PermissionDeniedException ("The owner of template is disabled: " + account );
242254 }
243255
244- return accountId ;
256+ return CallContext . current (). getCallingAccount (). getId () ;
245257 }
246258
247259 @ Override
@@ -267,7 +279,7 @@ protected boolean isBareMetal() {
267279 public void create () throws ResourceAllocationException {
268280 VirtualMachineTemplate template = null ;
269281 //TemplateOwner should be the caller https://issues.citrite.net/browse/CS-17530
270- template = _templateService .createPrivateTemplateRecord (this , CallContext . current (). getCallingAccount ( ));
282+ template = _templateService .createPrivateTemplateRecord (this , _accountService . getAccount ( getEntityOwnerId () ));
271283 if (template != null ) {
272284 setEntityId (template .getId ());
273285 setEntityUuid (template .getUuid ());
0 commit comments