1// @ts-nocheck 2/* 3 * Copyright (c) 2022 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 osAccount from '@ohos.account.osAccount' 18import commonEvent from '@ohos.commonEvent'; 19import util from '@ohos.util'; 20import {Callback} from 'basic'; 21import Trace from '../../../../../../../../common/src/main/ets/default/Trace'; 22import {SysFaultLogger, FaultID} from '../../../../../../../../common/src/main/ets/default/SysFaultLogger'; 23import Log from '../../../../../../../../common/src/main/ets/default/Log'; 24import { CommonEventManager, getCommonEventManager } from "../../../../../../../../common/src/main/ets/default/commonEvent/CommonEventManager"; 25import EventManager from "../../../../../../../../common/src/main/ets/default/event/EventManager"; 26import { obtainLocalEvent } from "../../../../../../../../common/src/main/ets/default/event/EventUtil"; 27import {UserData} from '../data/userData'; 28 29const TAG = "ScreenLock-AccountsModel" 30const TYPE_ADMIN = 0; 31const TYPE_NORMAL = 1; 32const TYPE_GUEST = 2; 33 34export const ACCOUNTS_REFRESH_EVENT = "Accounts_Refresh_Event"; 35 36export enum AuthType { 37 //Authentication type pin. 38 PIN = 1, 39 //Authentication type face. 40 FACE = 2 41} 42 43export enum AuthSubType { 44 //Authentication sub type six number pin. 45 PIN_SIX = 10000, 46 //Authentication sub type self defined number pin. 47 PIN_NUMBER = 10001, 48 //Authentication sub type mixed number pin. 49 PIN_MIXED = 10002, 50 //Authentication sub type 2D face. 51 FACE_2D = 20000, 52 //Authentication sub type 3D face. 53 FACE_3D = 20001 54} 55 56export enum AuthTurstLevel { 57 //Authentication result trusted level 1. 58 ATL1 = 10000, 59 //Authentication result trusted level 2. 60 ATL2 = 20000, 61 //Authentication result trusted level 3. 62 ATL3 = 30000, 63 //Authentication result trusted level 4. 64 ATL4 = 40000 65} 66 67export enum GetPropertyType { 68 //Authentication remain times. 69 AUTH_SUB_TYPE = 1, 70 //Authentication remain times. 71 REMAIN_TIMES = 2, 72 //Authentication remain times. 73 FREEZING_TIME = 3 74} 75 76export enum ResultCode { 77 //success 78 SUCCESS = 0, 79 //fails 80 FAIL = 1, 81} 82const USER_SUBSCRIBE_INFO = { 83 events: [ 84 commonEvent.Support.COMMON_EVENT_USER_REMOVED, 85 ], 86}; 87 88export default class AccountsModel { 89 userAuthManager = new osAccount.UserAuth(); 90 pinAuthManager = new osAccount.PINAuth(); 91 mCurrentUserId: number = 100 92 private mManager?: CommonEventManager; 93 modelInit() { 94 Log.showDebug(TAG, "start ModelInit") 95 } 96 97 @SysFaultLogger({FAULT_ID: FaultID.ACCOUNT_SYSTEM, MSG: "call func on failed"}) 98 eventListener(typeName: "activate" | "activating", name: string, callback: Callback<void>) { 99 Log.showInfo(TAG, `eventListener:typeName ${typeName}`); 100 osAccount.getAccountManager().on(typeName, name, (userId: number) => { 101 Log.showInfo(TAG, `on ${typeName} callback userId = ${userId}`) 102 if (typeName == "activate"){ 103 this.mCurrentUserId = userId 104 } 105 callback() 106 }) 107 } 108 109 commonEventListener(callback: Callback<void>) { 110 this.mManager = getCommonEventManager( 111 TAG, 112 USER_SUBSCRIBE_INFO, 113 (data) => { 114 Log.showInfo(TAG, `USER_REMOVED. ${JSON.stringify(data)}`); 115 callback(); 116 }, 117 ); 118 this.mManager.subscriberCommonEvent(); 119 120 Log.showInfo(TAG, `commonEventListener eventListener`); 121 let subscribeInfo = {events: ['usual.event.USER_REMOVED']}; 122 commonEvent.createSubscriber(subscribeInfo, (error, subscriber) => { 123 Log.showInfo(TAG, `createSubscriber success. ${JSON.stringify(subscriber)}`); 124 commonEvent.subscribe(subscriber, (err, commonEventData) => { 125 Log.showInfo(TAG, `USER_REMOVED subscribe. ${JSON.stringify(commonEventData)}`); 126 callback(); 127 }); 128 }); 129 } 130 131 commonEventCancelListener() { 132 Log.showInfo(TAG, "cancel commonEvent"); 133 this.mManager?.release(); 134 this.mManager = undefined; 135 } 136 137 @SysFaultLogger({FAULT_ID: FaultID.ACCOUNT_SYSTEM, MSG: "call func off failed"}) 138 eventCancelListener(typeName: "activate" | "activating", name: string) { 139 Log.showInfo(TAG, `eventCancleListener:typeName ${typeName}`); 140 osAccount.getAccountManager().off(typeName, name); 141 } 142 143 updateAllUsers() { 144 this.addAllUsers(); 145 } 146 147 @SysFaultLogger({FAULT_ID: FaultID.ACCOUNT_SYSTEM, MSG: "call func queryAllCreatedOsAccounts failed"}) 148 private addAllUsers() { 149 Log.showDebug(TAG, "start getAllUsers"); 150 osAccount.getAccountManager().queryAllCreatedOsAccounts().then((list) => { 151 Log.showDebug(TAG, "start sort"); 152 let accountList = []; 153 let accountMap = new Map(); 154 list.sort(this.sortAccount.bind(this)); 155 for (const user of list) { 156 Log.showDebug(TAG, `start get user, localId=${user.localId}, localName=${user.localName}`); 157 if (user.isActived) { 158 this.mCurrentUserId = user.localId 159 } 160 let userData: UserData = { 161 userId: user.localId, 162 userName: user.localName, 163 userIconPath: "" 164 } 165 accountList.push(userData) 166 accountMap.set(user.localId, userData) 167 osAccount.getAccountManager().getOsAccountProfilePhoto(user.localId).then((path) => { 168 Log.showDebug(TAG, "start get photo:" + path); 169 accountMap.get(user.localId).userIconPath = path; 170 }) 171 } 172 EventManager.publish(obtainLocalEvent(ACCOUNTS_REFRESH_EVENT, accountList)); 173 }) 174 } 175 176 private sortAccount(info1, info2): number { 177 if (info1.isActived || info2.isActived) { 178 return info1.isActived ? -1 : 1; 179 } else if (info1.type.ADMIN == TYPE_ADMIN || info2.type.ADMIN == TYPE_ADMIN) { 180 return info1.type.ADMIN == TYPE_ADMIN ? -1 : 1; 181 } else if (info1.type.GUEST == TYPE_GUEST || info2.type.GUEST == TYPE_GUEST) { 182 return info1.type.GUEST == TYPE_GUEST ? 1 : -1; 183 } else { 184 return info2.localId - info1.localId; 185 } 186 } 187 188 @SysFaultLogger({FAULT_ID: FaultID.ACCOUNT_SYSTEM, MSG: "call func activateOsAccount failed"}) 189 onUserSwitch(userId: number) { 190 Log.showDebug(TAG, "onUserSwitch:" + userId) 191 osAccount.getAccountManager().activateOsAccount(userId).then(() => { 192 Log.showInfo(TAG, "activateOsAccount : " + userId); 193 }) 194 } 195 196 authUser(challenge, authType: AuthType, authLevel: number, callback) { 197 Log.showDebug(TAG, `authUser param: userId ${this.mCurrentUserId} challenge ${challenge}`); 198 Trace.end(Trace.CORE_METHOD_CALL_ACCOUNT_SYSTEM); 199 let challengeArray = new Uint8Array(challenge); 200 try { 201 this.userAuthManager.authUser(this.mCurrentUserId, challengeArray, authType, authLevel, { 202 onResult: (result, extraInfo) => { 203 Log.showInfo(TAG, `authUser UserAuthManager.authUser onResult`); 204 Trace.start(Trace.CORE_METHOD_PASS_ACCOUNT_SYSTEM_RESULT); 205 callback(result, extraInfo); 206 }, 207 onAcquireInfo: (moduleId, acquire, extraInfo) => { 208 Log.showInfo(TAG, `authUser UserAuthManager.authUser onAcquireInfo`); 209 } 210 } 211 ) 212 } catch(error) { 213 console.error(`authUser failed, code is ${error.code}, message is ${error.message}`); 214 } 215 } 216 217 getAuthProperty(authType, callback) { 218 Log.showDebug(TAG, `getAuthProperty param: authType ${authType}`); 219 let keyArray = [GetPropertyType.AUTH_SUB_TYPE, GetPropertyType.REMAIN_TIMES, GetPropertyType.FREEZING_TIME] 220 let request = { 221 'authType': authType, 222 'keys': keyArray 223 } 224 try { 225 this.userAuthManager.getProperty(request).then((properties) => { 226 Log.showInfo(TAG, `getAuthProperty properties ${JSON.stringify(properties)}`); 227 callback(properties) 228 }) 229 } catch (error) { 230 console.error(`getProperty failed, code is ${error.code}, message is ${error.message}`); 231 }; 232 } 233 234 registerPWDInputer(password: string): Promise<void> { 235 Log.showDebug(TAG, `registerPWDInputer`); 236 let result = this.registerInputer(password); 237 if (result) { 238 return Promise.resolve(); 239 } else { 240 return Promise.reject(); 241 } 242 } 243 244 private registerInputer(password: string): boolean { 245 Log.showDebug(TAG, `registerInputer`); 246 let result = null 247 try { 248 result = this.pinAuthManager.registerInputer({ 249 onGetData: (passType, inputData) => { 250 Log.showDebug(TAG, `registerInputer onSetData passType:${passType}`); 251 let textEncoder = new util.TextEncoder(); 252 let uint8PW = textEncoder.encode(password); 253 Log.showDebug(TAG, `registerInputer onSetData call`); 254 inputData.onSetData(passType, uint8PW); 255 } 256 }) 257 } catch(error) { 258 console.error(`registerInputer failed, code is ${e.code}, message is ${e.message}`); 259 } 260 261 Log.showInfo(TAG, `registerInputer result:${result}`); 262 return result; 263 } 264 265 unregisterInputer() { 266 Log.showDebug(TAG, `unregisterInputer`); 267 try { 268 this.pinAuthManager.unregisterInputer(); 269 } catch { 270 LogUtil.debug(`${this.TAG}unregisterInputer failed`); 271 } 272 } 273 274 modelFinish() { 275 Log.showDebug(TAG, "start modelFinish") 276 } 277 278 @SysFaultLogger({FAULT_ID: FaultID.ACCOUNT_SYSTEM, MSG: "call func isOsAccountActived failed"}) 279 isActivateAccount(callback: Callback<boolean>) { 280 Log.showDebug(TAG, `isActivateAccount userId:${this.mCurrentUserId}`) 281 osAccount.getAccountManager().isOsAccountActived(this.mCurrentUserId).then((isActivate) => { 282 Log.showInfo(TAG, `isActivateAccount userId:${this.mCurrentUserId} result: ${isActivate}`) 283 callback(isActivate) 284 }) 285 } 286 getCurrentUserId() { 287 return this.mCurrentUserId; 288 } 289}