1/** 2 * @file Describe the file 3 * Copyright (c) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import { Constants } from '@ohos/datastructure/src/main/ets/Constants'; 18import { 19 CalendarsColumns, 20 DEFAULT_CALENDAR_VALUE 21} from '@ohos/datastructure/src/main/ets/calendars/CalendarsColumns'; 22 23const TAG = 'DatabaseProcessorHelper'; 24 25/** 26 * 初始化数据中的 creator。保护 creator 字段无法被应用自定义插入 27 * 28 * @param values 插入的数据 29 * @param creator 调用接口的应用 30 */ 31export function initValueCreator(values, creator) { 32 values[Constants.CREATOR] = creator; 33} 34 35/** 36 * 低级别权限下无法操作其他应用的账户。接口的 predicates 中带上其他应用的信息会冲突,无法进行操作 37 * 38 * @param predicates 39 * @param creator 调用接口的应用 40 */ 41export function initPredicateCreator(predicates, creator) { 42 predicates.equalTo(Constants.CREATOR, creator); 43} 44 45/** 46 * 低权限只能查找默认账户和自己应用下的账户。接口的 predicates 中带上应用的信息会冲突,无法进行操作 47 * 48 * @param predicates 49 * @param creator 调用接口的应用 50 */ 51export function initPredicateDefaultAndOwnCreator(predicates, creator) { 52 predicates.beginWrap() 53 .equalTo(Constants.CREATOR, creator) 54 .or() 55 .beginWrap() 56 .equalTo(CalendarsColumns.ACCOUNT_TYPE, DEFAULT_CALENDAR_VALUE.account_type) 57 .and() 58 .equalTo(CalendarsColumns.ACCOUNT_NAME, DEFAULT_CALENDAR_VALUE.account_name) 59 .and() 60 .equalTo(Constants.CREATOR, DEFAULT_CALENDAR_VALUE.creator) 61 .endWrap() 62 .endWrap(); 63} 64 65/** 66 * 删除数据中的 creator 字段。保护 creator 字段无法被修改 67 * 68 * @param values 更新的数据 69 */ 70export function deleteValueCreator(values) { 71 if (Object.hasOwnProperty.call(values, Constants.CREATOR)) { 72 Reflect.deleteProperty(values, Constants.CREATOR); 73 } 74} 75 76/** 77 * 保护默认账户无法被修改和删除。接口的 predicates 中带上默认账户的信息会冲突,无法进行操作 78 * 79 * @param predicates 80 */ 81export function initPredicateDefaultCalendarProtect(predicates) { 82 predicates.beginWrap() 83 .notEqualTo(CalendarsColumns.ACCOUNT_TYPE, DEFAULT_CALENDAR_VALUE.account_type) 84 .or() 85 .notEqualTo(CalendarsColumns.ACCOUNT_NAME, DEFAULT_CALENDAR_VALUE.account_name) 86 .or() 87 .notEqualTo(Constants.CREATOR, DEFAULT_CALENDAR_VALUE.creator) 88 .endWrap(); 89}