Search in sources :

Example 1 with UpdateIsoPermissionsCmd

use of com.cloud.api.command.user.iso.UpdateIsoPermissionsCmd in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method updateTemplateOrIsoPermissions.

@DB
@Override
public boolean updateTemplateOrIsoPermissions(final BaseUpdateTemplateOrIsoPermissionsCmd cmd) {
    // Input validation
    final Long id = cmd.getId();
    final Account caller = CallContext.current().getCallingAccount();
    List<String> accountNames = cmd.getAccountNames();
    final List<Long> projectIds = cmd.getProjectIds();
    final Boolean isFeatured = cmd.isFeatured();
    final Boolean isPublic = cmd.isPublic();
    final Boolean isExtractable = cmd.isExtractable();
    final String operation = cmd.getOperation();
    String mediaType = "";
    final VMTemplateVO template = _tmpltDao.findById(id);
    if (template == null) {
        throw new InvalidParameterValueException("unable to find " + mediaType + " with id " + id);
    }
    if (cmd instanceof UpdateTemplatePermissionsCmd) {
        mediaType = "template";
        if (template.getFormat().equals(ImageFormat.ISO)) {
            throw new InvalidParameterValueException("Please provide a valid template");
        }
    }
    if (cmd instanceof UpdateIsoPermissionsCmd) {
        mediaType = "iso";
        if (!template.getFormat().equals(ImageFormat.ISO)) {
            throw new InvalidParameterValueException("Please provide a valid iso");
        }
    }
    // convert projectIds to accountNames
    if (projectIds != null) {
        // CS-17842, initialize accountNames list
        if (accountNames == null) {
            accountNames = new ArrayList<>();
        }
        for (final Long projectId : projectIds) {
            final Project project = _projectMgr.getProject(projectId);
            if (project == null) {
                throw new InvalidParameterValueException("Unable to find project by id " + projectId);
            }
            if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
                throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId);
            }
            accountNames.add(_accountMgr.getAccount(project.getProjectAccountId()).getAccountName());
        }
    }
    // _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template);
    // TODO: should we replace all ModifyEntry as OperateEntry?
    _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template);
    // If the template is removed throw an error.
    if (template.getRemoved() != null) {
        s_logger.error("unable to update permissions for " + mediaType + " with id " + id + " as it is removed  ");
        throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id + " as it is removed ");
    }
    if (id.equals(Long.valueOf(1))) {
        throw new InvalidParameterValueException("unable to update permissions for " + mediaType + " with id " + id);
    }
    final boolean isAdmin = _accountMgr.isAdmin(caller.getId());
    // check configuration parameter(allow.public.user.templates) value for
    // the template owner
    final boolean allowPublicUserTemplates = AllowPublicUserTemplates.valueIn(template.getAccountId());
    if (!isAdmin && !allowPublicUserTemplates && isPublic != null && isPublic) {
        throw new InvalidParameterValueException("Only private " + mediaType + "s can be created.");
    }
    if (accountNames != null) {
        if (operation == null || !operation.equalsIgnoreCase("add") && !operation.equalsIgnoreCase("remove") && !operation.equalsIgnoreCase("reset")) {
            throw new InvalidParameterValueException("Invalid operation on accounts, the operation must be either 'add' or 'remove' in order to modify launch permissions." + "  Given operation is: '" + operation + "'");
        }
    }
    final Long ownerId = template.getAccountId();
    if (ownerId == null) {
        // publishing to individual users is irrelevant
        throw new InvalidParameterValueException("Update template permissions is an invalid operation on template " + template.getName());
    }
    // Only admin or owner of the template should be able to change its permissions
    if (caller.getId() != ownerId && !isAdmin) {
        throw new InvalidParameterValueException("Unable to grant permission to account " + caller.getAccountName() + " as it is neither admin nor owner or the template");
    }
    VMTemplateVO updatedTemplate = _tmpltDao.createForUpdate();
    if (isPublic != null) {
        updatedTemplate.setPublicTemplate(isPublic.booleanValue());
    }
    if (isFeatured != null) {
        updatedTemplate.setFeatured(isFeatured.booleanValue());
    }
    if (isExtractable != null) {
        // Only Root admins allowed to change it for templates
        if (!template.getFormat().equals(ImageFormat.ISO) && !_accountMgr.isRootAdmin(caller.getId())) {
            throw new InvalidParameterValueException("Only ROOT admins are allowed to modify isExtractable attribute.");
        } else {
            // For Isos normal user can change it, as their are no derivatives.
            updatedTemplate.setExtractable(isExtractable.booleanValue());
        }
    }
    _tmpltDao.update(template.getId(), updatedTemplate);
    // when operation is add/remove, accountNames can not be null
    if (("add".equalsIgnoreCase(operation) || "remove".equalsIgnoreCase(operation)) && accountNames == null) {
        throw new InvalidParameterValueException("Operation " + operation + " requires accounts or projectIds to be passed in");
    }
    // Derive the domain id from the template owner as updateTemplatePermissions is not cross domain operation
    final Account owner = _accountMgr.getAccount(ownerId);
    final Domain domain = _domainDao.findById(owner.getDomainId());
    if ("add".equalsIgnoreCase(operation)) {
        final List<String> accountNamesFinal = accountNames;
        final List<Long> accountIds = new ArrayList<>();
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                for (final String accountName : accountNamesFinal) {
                    final Account permittedAccount = _accountDao.findActiveAccount(accountName, domain.getId());
                    if (permittedAccount != null) {
                        if (permittedAccount.getId() == caller.getId()) {
                            // don't grant permission to the template
                            continue;
                        // owner, they implicitly have permission
                        }
                        accountIds.add(permittedAccount.getId());
                        final LaunchPermissionVO existingPermission = _launchPermissionDao.findByTemplateAndAccount(id, permittedAccount.getId());
                        if (existingPermission == null) {
                            final LaunchPermissionVO launchPermission = new LaunchPermissionVO(id, permittedAccount.getId());
                            _launchPermissionDao.persist(launchPermission);
                        }
                    } else {
                        throw new InvalidParameterValueException("Unable to grant a launch permission to account " + accountName + " in domain id=" + domain.getUuid() + ", account not found.  " + "No permissions updated, please verify the account names and retry.");
                    }
                }
            }
        });
    } else if ("remove".equalsIgnoreCase(operation)) {
        final List<Long> accountIds = new ArrayList<>();
        for (final String accountName : accountNames) {
            final Account permittedAccount = _accountDao.findActiveAccount(accountName, domain.getId());
            if (permittedAccount != null) {
                accountIds.add(permittedAccount.getId());
            }
        }
        _launchPermissionDao.removePermissions(id, accountIds);
    } else if ("reset".equalsIgnoreCase(operation)) {
        // do we care whether the owning account is an admin? if the
        // owner is an admin, will we still set public to false?
        updatedTemplate = _tmpltDao.createForUpdate();
        updatedTemplate.setPublicTemplate(false);
        updatedTemplate.setFeatured(false);
        _tmpltDao.update(template.getId(), updatedTemplate);
        _launchPermissionDao.removeAllPermissions(id);
        _messageBus.publish(_name, TemplateManager.MESSAGE_RESET_TEMPLATE_PERMISSION_EVENT, PublishScope.LOCAL, template.getId());
    }
    return true;
}
Also used : Account(com.cloud.user.Account) VMTemplateVO(com.cloud.storage.VMTemplateVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) UpdateTemplatePermissionsCmd(com.cloud.api.command.user.template.UpdateTemplatePermissionsCmd) LaunchPermissionVO(com.cloud.storage.LaunchPermissionVO) Project(com.cloud.projects.Project) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) UpdateIsoPermissionsCmd(com.cloud.api.command.user.iso.UpdateIsoPermissionsCmd) ArrayList(java.util.ArrayList) List(java.util.List) Domain(com.cloud.domain.Domain) DB(com.cloud.utils.db.DB)

Aggregations

UpdateIsoPermissionsCmd (com.cloud.api.command.user.iso.UpdateIsoPermissionsCmd)1 UpdateTemplatePermissionsCmd (com.cloud.api.command.user.template.UpdateTemplatePermissionsCmd)1 Domain (com.cloud.domain.Domain)1 Project (com.cloud.projects.Project)1 LaunchPermissionVO (com.cloud.storage.LaunchPermissionVO)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 Account (com.cloud.user.Account)1 DB (com.cloud.utils.db.DB)1 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1