Browse Source

use common function when creating new user from user list and cli

iamsaltedfish 3 years ago
parent
commit
2c9dffe67b

+ 2 - 2
resources/email/newuser.tpl

@@ -130,8 +130,8 @@
                                                style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; text-align: left; width: 80%; margin: 40px auto;">
                                             <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
                                                 <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 5px 0;"
-                                                    valign="top">Hi, {$user->user_name}<br
-                                                            style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"/>邮箱: {$user->email}
+                                                    valign="top">Hi, <br
+                                                            style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"/>
                                                 </td>
                                             </tr>
                                             <tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">

+ 3 - 23
resources/views/material/admin/user/index.tpl

@@ -30,29 +30,11 @@
                                 <input class="form-control maxwidth-edit" id="quick_create" type="text">
                             </div>
                         </div>
-                        <div class="card-inner">
-                            <div class="form-group form-group-label">
-                                <label for="new_user_add_shop">
-                                    <label class="floating-label" for="new_user_add_shop"> 是否添加套餐 </label>
-                                    <select id="new_user_add_shop" class="form-control maxwidth-edit">
-                                        <option value="0">不添加</option>
-                                        {foreach $shops as $shop}
-                                        <option value="{$shop->id}">{$shop->name}</option>
-                                        {/foreach}
-                                    </select>
-                                </label>
-                            </div>
-                        </div>
-                        <div class="card-inner">
-                            <div class="form-group form-group-label">
-                                <label class="floating-label" for="new_user_money"> 用户余额「-1为按默认设置,其他为指定值」 </label>
-                                <input class="form-control maxwidth-edit" id="new_user_money" type="text" value="-1">
-                            </div>
-                        </div>
                         <div class="card-action">
                             <div class="card-action-btn pull-left">
-                                <a class="btn btn-flat waves-attach waves-light" id="quick_create_confirm"><span
-                                            class="icon">check</span>&nbsp;创建</a>
+                                <a class="btn btn-flat waves-attach waves-light" id="quick_create_confirm">
+                                    <span class="icon">check</span>&nbsp;创建
+                                </a>
                             </div>
                         </div>
                     </div>
@@ -254,8 +236,6 @@
                 dataType: 'json',
                 data: {
                     email: $$getValue('quick_create'),
-                    balance: $$getValue('new_user_money'),
-                    product: $$getValue('new_user_add_shop')
                 },
                 success: data => {
                     $("#result").modal();

+ 13 - 38
src/Command/User.php

@@ -1,5 +1,4 @@
 <?php
-
 namespace App\Command;
 
 use Exception;
@@ -10,6 +9,7 @@ use Ramsey\Uuid\Uuid;
 use App\Services\Config;
 use App\Models\Setting;
 use App\Models\User as ModelsUser;
+use App\Controllers\AuthController;
 
 class User extends Command
 {
@@ -148,7 +148,7 @@ class User extends Command
             if ($passwd == null) {
                 die("必须输入管理员密码.\r\n");
             }
-            
+
             fwrite(STDOUT, "(3/3) 按 Y 或 y 确认创建:");
             $y = trim(fgets(STDIN));
         } elseif (count($this->argv) === 5) {
@@ -157,45 +157,20 @@ class User extends Command
         }
 
         if (strtolower($y) == 'y') {
-            $current_timestamp          = time();
-            // create admin user
-            $configs = Setting::getClass('register');
-            // do reg user
-            $user                   = new ModelsUser();
-            $user->user_name        = 'admin';
-            $user->email            = $email;
-            $user->pass             = Hash::passwordHash($passwd);
-            $user->passwd           = Tools::genRandomChar(16);
-            $user->uuid             = Uuid::uuid3(Uuid::NAMESPACE_DNS, $email . '|' . $current_timestamp);
-            $user->port             = Tools::getLastPort() + 1;
-            $user->t                = 0;
-            $user->u                = 0;
-            $user->d                = 0;
-            $user->transfer_enable  = Tools::toGB($configs['sign_up_for_free_traffic']);
-            $user->invite_num       = $configs['sign_up_for_invitation_codes'];
-            $user->ref_by           = 0;
-            $user->is_admin         = 1;
-            $user->expire_in        = date('Y-m-d H:i:s', time() + $configs['sign_up_for_free_time'] * 86400);
-            $user->reg_date         = date('Y-m-d H:i:s');
-            $user->money            = 0;
-            $user->im_type          = 1;
-            $user->im_value         = '';
-            $user->class            = 0;
-            $user->node_speedlimit  = 0;
-            $user->theme            = $_ENV['theme'];
-
-            $ga                     = new GA();
-            $secret                 = $ga->createSecret();
-            $user->ga_token         = $secret;
-            $user->ga_enable        = 0;
+            try {
+                AuthController::register_helper('admin', $email, $passwd, '', '1', '', 0, false);
+            } catch (\Exception $e) {
+                $error_msg = $e->getMessage();
+            }
 
-            if ($user->save()) {
-                echo '创建成功,请在主页登录' . PHP_EOL;
-            } else {
-                echo '创建失败,请检查数据库配置' . PHP_EOL;
+            if (!empty($error_msg)) {
+                echo PHP_EOL . '创建失败,以下是错误信息:' . PHP_EOL;
+                die($error_msg);
             }
+
+            echo PHP_EOL . '创建成功,请在主页登录' . PHP_EOL;
         } else {
-            echo '已取消创建' . PHP_EOL;
+            echo PHP_EOL . '已取消创建' . PHP_EOL;
         }
     }
 

+ 32 - 112
src/Controllers/Admin/UserController.php

@@ -1,32 +1,23 @@
 <?php
-
 namespace App\Controllers\Admin;
 
-use App\Controllers\AdminController;
-use App\Models\{
-    User,
-    Shop,
-    Bought,
-    Setting,
-    DetectBanLog
-};
-use App\Services\{
-    Auth,
-    Mail,
-    Config
-};
-use App\Utils\{
-    GA,
-    Hash,
-    Tools,
-    Cookie
-};
+use App\Models\Bought;
+use App\Models\DetectBanLog;
+use App\Models\Setting;
+use App\Models\Shop;
+use App\Models\User;
+use App\Services\Auth;
+use App\Services\Mail;
+use App\Utils\Cookie;
+use App\Utils\GA;
+use App\Utils\Hash;
+use App\Utils\Tools;
 use Exception;
 use Ramsey\Uuid\Uuid;
-use Slim\Http\{
-    Request,
-    Response
-};
+use Slim\Http\Request;
+use Slim\Http\Response;
+use App\Controllers\AuthController;
+use App\Controllers\AdminController;
 
 class UserController extends AdminController
 {
@@ -89,106 +80,35 @@ class UserController extends AdminController
      */
     public function createNewUser($request, $response, $args)
     {
-        $email   = strtolower(trim($request->getParam('email')));
-        $money   = (int) trim($request->getParam('balance'));
-        $shop_id = (int) $request->getParam('product');
-
-        $user = User::where('email', $email)->first();
-        if ($user != null) {
+        $email = strtolower(trim($request->getParam('email')));
+        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
             return $response->withJson([
                 'ret' => 0,
-                'msg' => '邮箱已经被注册了'
+                'msg' => '邮箱不规范'
             ]);
         }
-
-        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
+        $user = User::where('email', $email)->first();
+        if ($user != null) {
             return $response->withJson([
                 'ret' => 0,
-                'msg' => '邮箱不规范'
+                'msg' => '此邮箱已注册'
             ]);
         }
 
-        $configs = Setting::getClass('register');
-        // do reg user
-        $user                       = new User();
-        $current_timestamp          = time();
-        $pass                       = Tools::genRandomChar();
-        $user->user_name            = $email;
-        $user->email                = $email;
-        $user->pass                 = Hash::passwordHash($pass);
-        $user->passwd               = Tools::genRandomChar(16);
-        $user->uuid                 = Uuid::uuid3(Uuid::NAMESPACE_DNS, $email . '|' . $current_timestamp);
-        $user->port                 = Tools::getAvPort();
-        $user->t                    = 0;
-        $user->u                    = 0;
-        $user->d                    = 0;
-        $user->method               = $configs['sign_up_for_method'];
-        $user->protocol             = $configs['sign_up_for_protocol'];
-        $user->protocol_param       = $configs['sign_up_for_protocol_param'];
-        $user->obfs                 = $configs['sign_up_for_obfs'];
-        $user->obfs_param           = $configs['sign_up_for_obfs_param'];
-        $user->forbidden_ip         = $_ENV['reg_forbidden_ip'];
-        $user->forbidden_port       = $_ENV['reg_forbidden_port'];
-        $user->im_type              = 2;
-        $user->im_value             = $email;
-        $user->transfer_enable      = Tools::toGB($configs['sign_up_for_free_traffic']);
-        $user->invite_num           = $configs['sign_up_for_invitation_codes'];
-        $user->auto_reset_day       = '0';
-        $user->auto_reset_bandwidth = '0';
-        $user->money                = ($money != -1 ? $money : 0);
-        $user->class_expire         = date('Y-m-d H:i:s', time() + $configs['sign_up_for_class_time'] * 86400);
-        $user->class                = $configs['sign_up_for_class'];
-        $user->node_connector       = $configs['connection_device_limit'];
-        $user->node_speedlimit      = $configs['connection_rate_limit'];
-        $user->expire_in            = date('Y-m-d H:i:s', time() + $configs['sign_up_for_free_time'] * 86400);
-        $user->reg_date             = date('Y-m-d H:i:s');
-        $user->reg_ip               = $_SERVER['REMOTE_ADDR'];
-        $user->theme                = $_ENV['theme'];
-
-        $groups = explode(',', $_ENV['random_group']);
-
-        $user->node_group = $groups[array_rand($groups)];
-
-        $ga = new GA();
-        $secret = $ga->createSecret();
+        $pwd = Tools::genRandomChar(16);
+        AuthController::register_helper('nickname', $email, $pwd, '', '1', '', 0, false);
 
-        $user->ga_token = $secret;
-        $user->ga_enable = 0;
-        if ($user->save()) {
-            $res['ret']         = 1;
-            $res['msg']         = '新用户注册成功 用户名: ' . $email . ' 随机初始密码: ' . $pass;
-            $res['email_error'] = 'success';
-            if ($shop_id > 0) {
-                $shop = Shop::find($shop_id);
-                if ($shop != null) {
-                    $bought           = new Bought();
-                    $bought->userid   = $user->id;
-                    $bought->shopid   = $shop->id;
-                    $bought->datetime = time();
-                    $bought->renew    = 0;
-                    $bought->coupon   = '';
-                    $bought->price    = $shop->price;
-                    $bought->save();
-                    $shop->buy($user);
-                } else {
-                    $res['msg'] .= '<br/>但是套餐添加失败了,原因是套餐不存在';
-                }
-            }
-            $subject            = $_ENV['appName'] . '-新用户注册通知';
-            $to                 = $user->email;
-            $text               = '您好,管理员已经为您生成账户,用户名: ' . $email . ',登录密码为:' . $pass . ',感谢您的支持。 ';
-            try {
-                Mail::send($to, $subject, 'newuser.tpl', [
-                    'user' => $user, 'text' => $text,
-                ], []);
-            } catch (Exception $e) {
-                $res['email_error'] = $e->getMessage();
-            }
-            return $response->withJson($res);
+        if (Setting::obtain('mail_driver') != 'none') {
+            $text = "使用以下信息登录:<br/>邮箱:$email <br/>密码:$pwd <br/>";
+            $subject = $_ENV['appName'] . ' - 你的账户';
+            Mail::send($email, $subject, 'newuser.tpl', [
+                'text' => $text,
+            ], []);
         }
+
         return $response->withJson([
-            'ret' => 0,
-            'msg' => '未知错误'
+            'ret' => 1,
+            'msg' => '用户添加成功,登录密码是:' . $pwd
         ]);
     }
 

+ 7 - 5
src/Controllers/AuthController.php

@@ -231,7 +231,7 @@ class AuthController extends BaseController
         ]);
     }
 
-    public function register_helper($name, $email, $passwd, $code, $imtype, $imvalue, $telegram_id)
+    public static function register_helper($name, $email, $passwd, $code, $imtype, $imvalue, $telegram_id, $auto_login = true)
     {
         $ga = new GA();
         $user = new User();
@@ -239,7 +239,7 @@ class AuthController extends BaseController
 
         $user->money = $_ENV['reg_money'];
         $user->email = $email;
-        $user->im_type = ($imtype == '') ? 'none' : $imtype;
+        $user->im_type = ($imtype == '') ? '1' : $imtype;
         $user->im_value = $antiXss->xss_clean($imvalue);
         $user->user_name = $antiXss->xss_clean($name);
         $user->port = Tools::getAvPort();
@@ -272,14 +272,16 @@ class AuthController extends BaseController
         $user->expire_in = date('Y-m-d H:i:s', time());
         $user->class_expire = date('Y-m-d H:i:s', time());
         $user->reg_date = date('Y-m-d H:i:s');
-        $user->reg_ip = $_SERVER['REMOTE_ADDR'];
+        $user->reg_ip = (empty($_SERVER['REMOTE_ADDR'])) ? '127.0.0.1' : $_SERVER['REMOTE_ADDR'];
         $user->theme = $_ENV['theme'];
         $groups = explode(',', $_ENV['random_group']);
         $user->node_group = $groups[array_rand($groups)];
         $user->save();
 
-        Auth::login($user->id, 3600);
-        $user->collectLoginIP($_SERVER['REMOTE_ADDR']);
+        if ($auto_login) {
+            Auth::login($user->id, 3600);
+            $user->collectLoginIP($_SERVER['REMOTE_ADDR']);
+        }
     }
 
     public function logout($request, $response, $next)