diff --git a/src/main/java/com/kingsrook/qbits/dataanalysis/model/DataAnalysisTables.java b/src/main/java/com/kingsrook/qbits/dataanalysis/model/DataAnalysisTables.java index 34b9327..dc9ae1e 100644 --- a/src/main/java/com/kingsrook/qbits/dataanalysis/model/DataAnalysisTables.java +++ b/src/main/java/com/kingsrook/qbits/dataanalysis/model/DataAnalysisTables.java @@ -237,7 +237,7 @@ public final class DataAnalysisTables .withField(field("kind", QFieldType.STRING).withPossibleValueSourceName("data_analysis_kind").withIsRequired(true)) .withField(field("definition_json", QFieldType.TEXT).withIsRequired(true)) .withField(field("chart_kind", QFieldType.STRING).withPossibleValueSourceName("data_analysis_chart_kind")) - .withField(field("owner_user_id", QFieldType.STRING).withIsRequired(true)) + .withField(field("owner_user_id", QFieldType.STRING).withIsRequired(true).withBehavior(InsertingUserBehavior.INSERTING_USER)) .withField(field("shared", QFieldType.BOOLEAN).withDefaultValue(false)) .withField(field("read_owner_user_id", QFieldType.STRING).withIsEditable(false).withIsHidden(true)) .withAuditRules(new QAuditRules().withAuditLevel(AuditLevel.FIELD)) @@ -251,7 +251,7 @@ public final class DataAnalysisTables tables.add(base(config, SAVED_STAR, "Stars", "star", "data_analysis.saved") .withRecordLabelFormatAndFields("%s", "user_id") .withField(field("saved_id", QFieldType.INTEGER).withPossibleValueSourceName("data_analysis_saved").withIsRequired(true)) - .withField(field("user_id", QFieldType.STRING).withIsRequired(true)) + .withField(field("user_id", QFieldType.STRING).withIsRequired(true).withBehavior(InsertingUserBehavior.INSERTING_USER)) .withUniqueKey(new UniqueKey("saved_id", "user_id")) .withRecordSecurityLock(ownerLock("user_id"))); tables.add(base(config, SCHEDULE, "Schedules", "schedule", "data_analysis.saved") @@ -285,7 +285,7 @@ public final class DataAnalysisTables .withField(field("date_field", QFieldType.STRING).withIsRequired(true)) .withField(field("bucket", QFieldType.STRING).withDefaultValue("DAY").withIsRequired(true).withPossibleValueSourceName("data_analysis_watch_bucket")) .withField(field("severity", QFieldType.STRING).withDefaultValue("WARNING").withIsRequired(true).withPossibleValueSourceName("data_analysis_watch_severity")) - .withField(field("owner_user_id", QFieldType.STRING).withIsRequired(true)) + .withField(field("owner_user_id", QFieldType.STRING).withIsRequired(true).withBehavior(InsertingUserBehavior.INSERTING_USER)) .withField(field("enabled", QFieldType.BOOLEAN).withDefaultValue(false)) .withField(field("last_evaluated_at", QFieldType.DATE_TIME).withIsEditable(false)) .withField(field("last_value", QFieldType.DECIMAL).withIsEditable(false)) @@ -311,7 +311,7 @@ public final class DataAnalysisTables .withField(field("sampled", QFieldType.BOOLEAN).withDefaultValue(false)) .withField(field("result_json", QFieldType.TEXT)) .withField(field("error", QFieldType.TEXT)) - .withField(field("user_id", QFieldType.STRING).withIsRequired(true)) + .withField(field("user_id", QFieldType.STRING).withIsRequired(true).withBehavior(InsertingUserBehavior.INSERTING_USER)) .withRecordSecurityLock(ownerLock("user_id"))); for(QTableMetaData table : tables) { diff --git a/src/main/java/com/kingsrook/qbits/dataanalysis/model/InsertingUserBehavior.java b/src/main/java/com/kingsrook/qbits/dataanalysis/model/InsertingUserBehavior.java new file mode 100644 index 0000000..926468f --- /dev/null +++ b/src/main/java/com/kingsrook/qbits/dataanalysis/model/InsertingUserBehavior.java @@ -0,0 +1,84 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2025. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.kingsrook.qbits.dataanalysis.model; + + +import java.util.List; +import com.kingsrook.qqq.backend.core.actions.values.ValueBehaviorApplier; +import com.kingsrook.qqq.backend.core.context.QContext; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import com.kingsrook.qqq.backend.core.model.metadata.fields.FieldBehavior; +import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData; +import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData; +import com.kingsrook.qqq.backend.core.utils.CollectionUtils; +import com.kingsrook.qqq.backend.core.utils.StringUtils; + + +/******************************************************************************* + ** On INSERT only, an owner/user field left empty gets the inserting user's id, + ** so a UI (which does not know the user's id) can create a saved analysis, + ** star or watch. Unlike core DynamicDefaultValueBehavior.USER_ID it never + ** applies on UPDATE, so an administrator's partial edit cannot take ownership. + ** Naming another owner is still refused by the table customizers and locks. + *******************************************************************************/ +public enum InsertingUserBehavior implements FieldBehavior +{ + INSERTING_USER, + NONE; + + + + /******************************************************************************* + ** No behavior unless asked for. + *******************************************************************************/ + @Override + public InsertingUserBehavior getDefault() + { + return (NONE); + } + + + + /******************************************************************************* + ** Fill empty values with the session user's id on insert. + *******************************************************************************/ + @Override + public void apply(ValueBehaviorApplier.Action action, List recordList, QInstance instance, QTableMetaData table, QFieldMetaData field) + { + if(this == NONE || action != ValueBehaviorApplier.Action.INSERT) + { + return; + } + String userId = QContext.getQSession() == null || QContext.getQSession().getUser() == null ? null : QContext.getQSession().getUser().getIdReference(); + if(!StringUtils.hasContent(userId)) + { + return; + } + for(QRecord record : CollectionUtils.nonNullList(recordList)) + { + if(!StringUtils.hasContent(record.getValueString(field.getName()))) + { + record.setValue(field.getName(), userId); + } + } + } +} diff --git a/src/test/java/com/kingsrook/qbits/dataanalysis/SavedAnalysesTest.java b/src/test/java/com/kingsrook/qbits/dataanalysis/SavedAnalysesTest.java index 9d5af3d..5604f25 100644 --- a/src/test/java/com/kingsrook/qbits/dataanalysis/SavedAnalysesTest.java +++ b/src/test/java/com/kingsrook/qbits/dataanalysis/SavedAnalysesTest.java @@ -87,6 +87,23 @@ class SavedAnalysesTest extends EngineFixture assertThat(api.list()).isEmpty(); } + /******************************************************************************* + ** A UI insert without an owner belongs to the inserting user; naming someone + ** else is still refused for a non-administrator. + *******************************************************************************/ + @Test + void insertWithoutOwnerBelongsToTheCurrentUser() throws Exception + { + QContext.init(instance, viewer("bob", 2)); + QRecord row = definition("bob", false, "COLUMN_PROFILE", Map.of("fieldName", "amount")); + row.removeValue("owner_user_id"); + QRecord inserted = new InsertAction().execute(new InsertInput(DataAnalysisTables.SAVED).withRecords(List.of(row))).getRecords().getFirst(); + assertThat(inserted.getErrors()).isEmpty(); + assertThat(inserted.getValueString("owner_user_id")).isEqualTo("bob"); + QRecord forged = definition("alice", false, "COLUMN_PROFILE", Map.of("fieldName", "amount")); + assertThat(new InsertAction().execute(new InsertInput(DataAnalysisTables.SAVED).withRecords(List.of(forged))).getRecords().getFirst().getErrors()).isNotEmpty(); + } + @Test void starsAreIdempotentAndPersonal() throws Exception {