package io.cattle.platform.iaas.api.object.postinit;

import io.cattle.platform.api.auth.Policy;
import io.cattle.platform.api.utils.ApiUtils;
import io.cattle.platform.core.constants.AccountConstants;
import io.cattle.platform.object.meta.ObjectMetaDataManager;
import io.cattle.platform.object.postinit.ObjectPostInstantiationHandler;
import io.cattle.platform.object.util.ObjectUtils;
import io.github.ibuildthecloud.gdapi.context.ApiContext;

import java.util.Map;

public class AccountFieldPostInitHandler implements ObjectPostInstantiationHandler {

    @Override
    public <T> T postProcess(T obj, Class<T> clz, Map<String, Object> properties) {
        ApiContext apiContext = ApiContext.getContext();
        if ( apiContext == null ) {
            /* Back-end can do whatever it wants */
            return obj;
        }

        Policy policy = ApiUtils.getPolicy();
        boolean overwrite = true;

        if ( policy.isOption(Policy.AUTHORIZED_FOR_ALL_ACCOUNTS) && properties.containsKey(AccountConstants.ACCOUNT_ID) ) {
            overwrite = false;
        }

        if ( overwrite ) {
            ObjectUtils.setPropertyIgnoreErrors(obj, ObjectMetaDataManager.ACCOUNT_FIELD, policy.getAccountId());
        }

        return obj;
    }

}

@@ -377,24 +377,70 @@
 bjectUtils;%0A
+import io.cattle.platform.util.type.Priority;%0A
 import io.gi
@@ -591,16 +591,26 @@
 nHandler
+, Priority
  %7B%0A%0A    
@@ -1309,10 +1309,95 @@
 %0A    %7D%0A%0A
+    @Override%0A    public int getPriority() %7B%0A        return Priority.DEFAULT;%0A    %7D%0A%0A
 %7D%0A

reorder object post init handlers