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