Browse Source

feat: user avatar by dicebear

M1Screw 2 years ago
parent
commit
49a6ea6ebf

+ 0 - 72
.github/workflows/codeql-analysis.yml

@@ -1,72 +0,0 @@
-# For most projects, this workflow file will not need changing; you simply need
-# to commit it to your repository.
-#
-# You may wish to alter this file to override the set of languages analyzed,
-# or to provide custom queries or build logic.
-#
-# ******** NOTE ********
-# We have attempted to detect the languages in your repository. Please check
-# the `language` matrix defined below to confirm you have the correct set of
-# supported CodeQL languages.
-#
-name: "CodeQL"
-
-on:
-  push:
-    branches: [ "dev" ]
-  pull_request:
-    # The branches below must be a subset of the branches above
-    branches: [ "dev" ]
-  schedule:
-    - cron: '34 4 * * 0'
-
-jobs:
-  analyze:
-    name: Analyze
-    runs-on: ubuntu-latest
-    permissions:
-      actions: read
-      contents: read
-      security-events: write
-
-    strategy:
-      fail-fast: false
-      matrix:
-        language: [ 'javascript' ]
-        # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
-        # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
-
-    steps:
-    - name: Checkout repository
-      uses: actions/checkout@v3
-
-    # Initializes the CodeQL tools for scanning.
-    - name: Initialize CodeQL
-      uses: github/codeql-action/init@v2
-      with:
-        languages: ${{ matrix.language }}
-        # If you wish to specify custom queries, you can do so here or in a config file.
-        # By default, queries listed here will override any specified in a config file.
-        # Prefix the list here with "+" to use these queries and those in the config file.
-        
-        # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
-        # queries: security-extended,security-and-quality
-
-        
-    # Autobuild attempts to build any compiled languages  (C/C++, C#, or Java).
-    # If this step fails, then you should remove it and run the build manually (see below)
-    - name: Autobuild
-      uses: github/codeql-action/autobuild@v2
-
-    # ℹ️ Command-line programs to run using the OS shell.
-    # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
-
-    #   If the Autobuild fails above, remove it and uncomment the following three lines. 
-    #   modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
-
-    # - run: |
-    #   echo "Run, Build Application using script"
-    #   ./location_of_script_within_repo/buildscript.sh
-
-    - name: Perform CodeQL Analysis
-      uses: github/codeql-action/analyze@v2

+ 1 - 1
.github/workflows/lint.yml

@@ -1,4 +1,4 @@
-name: Lint php code
+name: Lint PHP Code
 on:
   push:
   pull_request:

+ 1 - 1
resources/views/tabler/admin/header.tpl

@@ -47,7 +47,7 @@
                         <a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown"
                             aria-label="Open user menu">
                             <span class="avatar avatar-sm"
-                                style="background-image: url({$user->gravatar})"></span>
+                                style="background-image: url({$user->dice_bear})"></span>
                             <div class="d-none d-xl-block ps-2">
                                 <div>{$user->email}</div>
                                 <div class="mt-1 small text-secondary">{$user->user_name}</div>

+ 1 - 1
resources/views/tabler/user/header.tpl

@@ -46,7 +46,7 @@
                         <a href="#" class="nav-link d-flex lh-1 text-reset p-0" data-bs-toggle="dropdown"
                             aria-label="Open user menu">
                             <span class="avatar avatar-sm"
-                                style="background-image: url({$user->gravatar})"></span>
+                                style="background-image: url({$user->dice_bear})"></span>
                             <div class="d-none d-xl-block ps-2">
                                 <div>{$user->email}</div>
                                 <div class="mt-1 small text-secondary">{$user->user_name}</div>

+ 6 - 1
src/Controllers/BaseController.php

@@ -39,7 +39,12 @@ abstract class BaseController
     public function view(): Smarty
     {
         if (View::$connection) {
-            $this->view->assign('queryLog', View::$connection->connection('default')->getQueryLog())->assign('optTime', (microtime(true) - View::$beginTime) * 1000);
+            $this->view->assign(
+                'queryLog',
+                View::$connection
+                    ->connection('default')
+                    ->getQueryLog()
+            )->assign('optTime', (microtime(true) - View::$beginTime) * 1000);
         }
         return $this->view;
     }

+ 3 - 6
src/Models/User.php

@@ -22,9 +22,7 @@ use function md5;
 use function random_int;
 use function round;
 use function str_replace;
-use function strtolower;
 use function time;
-use function trim;
 use const PHP_EOL;
 
 final class User extends Model
@@ -52,12 +50,11 @@ final class User extends Model
     ];
 
     /**
-     * Gravatar 头像地址
+     * DiceBear 头像
      */
-    public function getGravatarAttribute(): string
+    public function getDiceBearAttribute(): string
     {
-        $hash = md5(strtolower(trim($this->email)));
-        return 'https://www.gravatar.com/avatar/' . $hash . '?&d=identicon';
+        return 'https://api.dicebear.com/6.x/identicon/svg?seed=' . md5($this->email);
     }
 
     /**

+ 58 - 0
tests/App/Utils/HashTest.php

@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Utils;
+
+use PHPUnit\Framework\TestCase;
+use App\Utils\Hash;
+
+class HashTest extends TestCase
+{
+    /**
+     * @covers App\Utils\Hash::cookieHash
+     */
+    public function testCookieHash()
+    {
+        $_ENV['key'] = 'cookie_key';
+        $passHash = 'password';
+        $expire_in = '1 hour';
+        $result = Hash::cookieHash($passHash, $expire_in);
+        $this->assertIsString($result);
+        $this->assertEquals(45, strlen($result));
+    }
+
+    /**
+     * @covers App\Utils\Hash::ipHash
+     */
+    public function testIpHash()
+    {
+        $_ENV['key'] = 'cookie_key';
+        $ip = '192.168.0.1';
+        $uid = 'user_id';
+        $expire_in = '1 hour';
+        $result = Hash::ipHash($ip, $uid, $expire_in);
+        $this->assertIsString($result);
+        $this->assertEquals(45, strlen($result));
+    }
+
+    /**
+     * @covers App\Utils\Hash::checkPassword
+     * @covers App\Utils\Hash::passwordHash
+     * @covers App\Utils\Hash::md5WithSalt
+     * @covers App\Utils\Hash::sha256WithSalt
+     */
+    public function testPasswordFunctions()
+    {
+        $_ENV['salt'] = 'password_salt';
+        $_ENV['pwdMethod'] = 'bcrypt';
+        $password = 'password';
+        $hashedPassword = Hash::passwordHash($password);
+        $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
+        $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
+        $this->assertIsString(Hash::passwordHash($password));
+        $this->assertIsString(Hash::md5WithSalt($password));
+        $this->assertIsString(Hash::sha256WithSalt($password));
+    }
+}
+