Created
March 17, 2024 09:22
-
-
Save Chenx221/75dbb2d2ff6771f700500bdf5351ab2a to your computer and use it in GitHub Desktop.
Revisions
-
Chenx221 created this gist
Mar 17, 2024 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ <?php namespace app\models; use JsonException; use Webauthn\PublicKeyCredentialSource; use Yii; use yii\db\ActiveQuery; use yii\db\ActiveRecord; /** * * @property int $id record id * @property int $user_id user id * @property string $name device name * @property string $public_key_credential_id PKCID * @property string $data PKC_DATA * * @property User $user */ class PublicKeyCredentialSourceRepository extends ActiveRecord { public static function tableName(): string { return 'public_key_credential_source_repository'; } public function rules(): array { return [ [['user_id', 'name', 'public_key_credential_id', 'data'], 'required'], [['user_id'], 'integer'], [['data'], 'string'], [['name'], 'string', 'max' => 64], [['public_key_credential_id'], 'string', 'max' => 255], [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], ]; } public function attributeLabels(): array { return [ 'id' => 'ID', 'user_id' => 'User ID', 'name' => 'Name', 'public_key_credential_id' => 'Public Key Credential ID', 'data' => 'Data', ]; } public function getUser(): ActiveQuery { return $this->hasOne(User::class, ['id' => 'user_id']); } public function findAllForUserEntity(User $user): array { return self::findAll(['user_id' => $user->id]); } public function findOneByCredentialId(string $PKC_ID): ?PublicKeyCredentialSourceRepository { return self::findOne(['public_key_credential_id' => $PKC_ID]); } public function saveCredential(PublicKeyCredentialSource $PKCS, string $name, bool $isNewRecord = true): bool { $jsonSerialize = $PKCS->jsonSerialize(); $this->public_key_credential_id = $jsonSerialize['publicKeyCredentialId']; $publicKeyCredentialSourceJson = json_encode($jsonSerialize, JSON_THROW_ON_ERROR); $this->data = $publicKeyCredentialSourceJson; if($isNewRecord){ $this->name = $name; } $this->user_id = Yii::$app->user->id; return $this->save(); } }