we are in a development phase, where we often have to restore a database from production environment and the set permissions by hand, since they are not in the production environment. The changes are so many, that we decided to make a script to update the products:
Code: Select all
import org.cyclos.impl.users.ProductServiceLocal
import org.cyclos.impl.utils.jdbc.ScriptHelper
import org.cyclos.model.users.products.ProductQuery
def result = []
result << 'Adding Records to products:'
def scriptHelper = new ScriptHelper()
def productQuery = new ProductQuery()
def products = (productService as ProductServiceLocal).listEntities()
result << 'Products found: ' + products.size()
// Configuration array: one entry per product/network/record-type to change
def permissionRules = [
[productInternalName: 'helpers', networkInternalName: 'austria', recordTypeInternalName: 'dD_chat', enable: true, view: true, create: true, edit: false, remove: false],
[productInternalName: 'helpers', networkInternalName: 'austria', recordTypeInternalName: 'dD_checklist', enable: true, view: true, create: false, edit: false, remove: false],
]
// Helper to format current permissions
def permissionsSummary = { rec ->
if (!rec.isEnabled()) return ' ---'
def tokens = ['Enabled']
if (rec.isView()) {
tokens << 'View'
if (rec.isCreate()) tokens << 'Create'
if (rec.isEdit()) tokens << 'Edit'
if (rec.isRemove()) tokens << 'Remove'
}
tokens. Join(',')
}
// Fast lookup for rules by "product::network::recordType"
def rulesByKey = permissionRules.collectEntries { r ->
[("${r.productInternalName}::${r.networkInternalName}::${r.recordTypeInternalName}".toString()): r]
}
// Explicitly typed set to avoid intersection-type issues
Set<String> productsWithRules = new LinkedHashSet<String>(
permissionRules.collect { it.productInternalName?.toString() }.findAll { it }
)
// Helper to also set the underlying action/permission set (when present)
def applyActionSetIfSupported = { rec ->
def actions = new LinkedHashSet()
if (rec.isView()) actions << 'VIEW'
if (rec.isCreate()) actions << 'CREATE'
if (rec.isEdit()) actions << 'EDIT'
if (rec.isRemove()) actions << 'REMOVE'
// Try common setters / properties. If not present, no harm done.
if (rec.respondsTo('setPermissions', Set)) {
rec.setPermissions(actions); return true
}
if (rec.respondsTo('setActions', Set)) {
rec.setActions(actions); return true
}
if (rec.hasProperty('permissions')) {
rec.permissions = actions; return true
}
if (rec.hasProperty('actions')) {
rec.actions = actions; return true
}
return false
}
products.each { product ->
// Skip products without any matching rule
if (!productsWithRules.contains(String.valueOf(product.internalName))) return
String networkName = product.network.internalName
// Skip if no rule for this product+network
if (!permissionRules.any { it.productInternalName == product.internalName && it.networkInternalName == networkName }) return
result << "Product: ${product.internalName} = ${product.name} für ${product.network.name} found"
product.getMyRecordTypes().each { recordPermission ->
def recordType = recordPermission.getRecordType()
result << ".... Record: ${recordType.internalName} current ${permissionsSummary(recordPermission)}"
def ruleKey = "${product.internalName}::${networkName}::${recordType.internalName}"
def rule = rulesByKey[ruleKey]
if (rule) {
if (rule.enable != null) recordPermission.setEnabled(rule.enable as boolean)
if (rule.view != null) recordPermission.setView(rule.view as boolean)
if (rule.create != null) recordPermission.setCreate(rule.create as boolean)
if (rule.edit != null) recordPermission.setEdit(rule.edit as boolean)
if (rule.remove != null) recordPermission.setRemove(rule.remove as boolean)
// Persist the underlying backing set when available
def applied = applyActionSetIfSupported(recordPermission)
if (!applied) {
result << "...... Note: could not locate a backing permission-set property; flags may not persist."
}
result << "...... Changed: ${recordType.internalName} now ${permissionsSummary(recordPermission)}"
}
}
}
// return result.join('\r\n')
Code: Select all
setEnable(true)Code: Select all
setView(true)I do get the "exception": Note: could not locate a backing permission-set property; flags may not persist.
If I run the script a second time, the current set shows, the changes were made.
My Cyclos is of 4.16.11
Any idea for this problem?
Thx Erich