Search in sources :

Example 1 with ListIsoPermissionsCmd

use of org.apache.cloudstack.api.command.user.iso.ListIsoPermissionsCmd in project cloudstack by apache.

the class TemplateManagerImpl method listTemplatePermissions.

@Override
public List<String> listTemplatePermissions(BaseListTemplateOrIsoPermissionsCmd cmd) {
    Account caller = CallContext.current().getCallingAccount();
    Long id = cmd.getId();
    if (id.equals(Long.valueOf(1))) {
        throw new PermissionDeniedException("unable to list permissions for " + cmd.getMediaType() + " with id " + id);
    }
    VirtualMachineTemplate template = _tmpltDao.findById(id);
    if (template == null) {
        throw new InvalidParameterValueException("unable to find " + cmd.getMediaType() + " with id " + id);
    }
    if (cmd instanceof ListTemplatePermissionsCmd) {
        if (template.getFormat().equals(ImageFormat.ISO)) {
            throw new InvalidParameterValueException("Please provide a valid template");
        }
    } else if (cmd instanceof ListIsoPermissionsCmd) {
        if (!template.getFormat().equals(ImageFormat.ISO)) {
            throw new InvalidParameterValueException("Please provide a valid iso");
        }
    }
    if (!template.isPublicTemplate()) {
        _accountMgr.checkAccess(caller, null, true, template);
    }
    List<String> accountNames = new ArrayList<String>();
    List<LaunchPermissionVO> permissions = _launchPermissionDao.findByTemplate(id);
    if ((permissions != null) && !permissions.isEmpty()) {
        for (LaunchPermissionVO permission : permissions) {
            Account acct = _accountDao.findById(permission.getAccountId());
            accountNames.add(acct.getAccountName());
        }
    }
    // also add the owner if not public
    if (!template.isPublicTemplate()) {
        Account templateOwner = _accountDao.findById(template.getAccountId());
        accountNames.add(templateOwner.getAccountName());
    }
    return accountNames;
}
Also used : Account(com.cloud.user.Account) ListTemplatePermissionsCmd(org.apache.cloudstack.api.command.user.template.ListTemplatePermissionsCmd) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) ListIsoPermissionsCmd(org.apache.cloudstack.api.command.user.iso.ListIsoPermissionsCmd) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) LaunchPermissionVO(com.cloud.storage.LaunchPermissionVO)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 LaunchPermissionVO (com.cloud.storage.LaunchPermissionVO)1 Account (com.cloud.user.Account)1 ArrayList (java.util.ArrayList)1 ListIsoPermissionsCmd (org.apache.cloudstack.api.command.user.iso.ListIsoPermissionsCmd)1 ListTemplatePermissionsCmd (org.apache.cloudstack.api.command.user.template.ListTemplatePermissionsCmd)1