mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
nexus clients
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class Test extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'test:test';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Just for test';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
dd(strlen(Hash::make('123456')));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\AgentAllowRequest;
|
||||
use App\Http\Resources\AgentAllowResource;
|
||||
use App\Models\AgentAllow;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -24,11 +25,13 @@ class AgentAllowController extends Controller
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store(AgentAllowRequest $request)
|
||||
{
|
||||
//
|
||||
$result = AgentAllow::query()->create($request->all());
|
||||
$resource = new AgentAllowResource($result);
|
||||
return success('agent allow store', $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,21 +50,26 @@ class AgentAllowController extends Controller
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
$result = AgentAllow::query()->findOrFail($id);
|
||||
$result->update($request->all());
|
||||
$resource = new AgentAllowResource($result);
|
||||
return success('agent allow update', $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
$result = AgentAllow::query()->findOrFail($id);
|
||||
$success = $result->delete();
|
||||
return success('agent allow delete', $success);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Models\User;
|
||||
use App\Repositories\UserRepository;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = (new UserRepository())->getList($request->all());
|
||||
$resource = UserResource::collection($result);
|
||||
return success('user list', $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$result = (new UserRepository())->store($request->all());
|
||||
$resource = new UserResource($result);
|
||||
return success('user store', $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AgentAllowRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'family' => 'required|string',
|
||||
'start_name' => 'required|string',
|
||||
'peer_id_pattern' => 'required|string',
|
||||
'peer_id_match_num' => 'required|numeric',
|
||||
'peer_id_matchtype' => 'required|in:hex,dec',
|
||||
'peer_id_start' => 'required|string',
|
||||
'agent_pattern' => 'required|string',
|
||||
'agent_match_num' => 'required|numeric',
|
||||
'agent_matchtype' => 'required|in:hex,dec',
|
||||
'agent_start' => 'required|string',
|
||||
'exception' => 'required|in:yes,no',
|
||||
'allowhttps' => 'required|in:yes,no',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'username' => $this->username,
|
||||
'uploaded' => $this->uploaded,
|
||||
'downloaded' => $this->downloaded,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,7 @@ class NexusModel extends Model
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
|
||||
protected $casts = [
|
||||
'added' => 'datetime'
|
||||
];
|
||||
}
|
||||
|
||||
+3
-6
@@ -17,9 +17,7 @@ class User extends Authenticatable
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'username', 'email', 'passhash', 'secret', 'status', 'added'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -28,8 +26,7 @@ class User extends Authenticatable
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
'passhash', 'secret'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -38,6 +35,6 @@ class User extends Authenticatable
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
'added' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class UserRepository
|
||||
{
|
||||
public function store(array $params)
|
||||
{
|
||||
$required = ['username', 'email', 'password', 'password_confirmation'];
|
||||
foreach ($required as $field) {
|
||||
if (empty($params[$field])) {
|
||||
throw new \InvalidArgumentException("Require $field");
|
||||
}
|
||||
}
|
||||
$username = $params['username'];
|
||||
$email = $params['email'];
|
||||
$password = $params['password'];
|
||||
$confirmPassword = $params['password_confirmation'];
|
||||
|
||||
if (!validusername($username)) {
|
||||
throw new \InvalidArgumentException("Invalid username: $username");
|
||||
}
|
||||
$email = htmlspecialchars(trim($email));
|
||||
$email = safe_email($email);
|
||||
if (!check_email($email)) {
|
||||
throw new \InvalidArgumentException("Invalid email: $email");
|
||||
}
|
||||
$exists = User::query()->where('email', $email)->exists();
|
||||
if ($exists) {
|
||||
throw new \InvalidArgumentException("The email address: $email is already in use");
|
||||
}
|
||||
if (mb_strlen($password) < 6 || mb_strlen($password) > 40) {
|
||||
throw new \InvalidArgumentException("Invalid password: $password, it should be more than 6 character and less than 40 character");
|
||||
}
|
||||
if ($password != $confirmPassword) {
|
||||
throw new \InvalidArgumentException("confirmPassword: $confirmPassword != password: $password");
|
||||
}
|
||||
$setting = get_setting('main');
|
||||
$secret = mksecret();
|
||||
$passhash = md5($secret . $password . $secret);
|
||||
$insert = [
|
||||
'username' => $username,
|
||||
'passhash' => $passhash,
|
||||
'secret' => $secret,
|
||||
'email' => $email,
|
||||
'stylesheet' => $setting['defstylesheet'],
|
||||
'status' => 'confirmed',
|
||||
'added' => now()->toDateTimeString(),
|
||||
];
|
||||
Log::info("create user: " . nexus_json_encode($insert));
|
||||
|
||||
return User::query()->create($insert);
|
||||
}
|
||||
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = User::query();
|
||||
$sortField = 'id';
|
||||
$validSortFields = ['uploaded', 'downloaded', ];
|
||||
if (!empty($params['sort']) && in_array($params['sort'], $validSortFields)) {
|
||||
$sortField = $params['sort'];
|
||||
}
|
||||
$fields = ['id', 'username', 'avatar', 'email', 'uploaded', 'downloaded', 'class', 'added'];
|
||||
if (!empty($params['fields'])) {
|
||||
$fields = $params['fields'];
|
||||
}
|
||||
|
||||
if (!empty($params['id'])) {
|
||||
$query->where('id', $params['id']);
|
||||
}
|
||||
if (!empty($params['username'])) {
|
||||
$query->where('username', $params['username']);
|
||||
}
|
||||
if (!empty($params['email'])) {
|
||||
$query->where('email', $params['email']);
|
||||
}
|
||||
|
||||
$result = $query->orderBy($sortField, 'desc')->select($fields)->paginate();
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user