Skip to content

Commit

Permalink
feat: Update session time handling and migration for user model
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Jan 31, 2025
1 parent 9ab6e47 commit 32b5c02
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/Http/Controllers/API/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ public function setInformation(Request $request)
'email' => 'required|string|email|max:255|unique:users,email,' . auth('api')->user()->id,
]);

$session_time = env('JWT_TTL', 120);
if ($request->session_time == $session_time) {
$session_time = -1;
}

$user->update([
'name' => $request->name,
'email' => $request->email,
'otp_enabled' => (bool) $request->otp_enabled,
'session_time' => $request->session_time,
'session_time' => $session_time,
]);

if (! (bool) $request->otp_enabled) {
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/API/Settings/TweaksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getConfiguration()
'LDAP_IGNORE_CERT' => (bool) env('LDAP_IGNORE_CERT', 'false'),
'LOGIN_IMAGE' => SystemSettings::where('key', 'LOGIN_IMAGE')->first()?->data ?? '',
'DEFAULT_AUTH_GATE' => env('DEFAULT_AUTH_GATE', 'liman'),
'JWT_TTL' => env('JWT_TTL', 120),
]);
}

Expand All @@ -47,6 +48,7 @@ public function saveConfiguration(Request $request)
'EXTENSION_TIMEOUT' => 'required|integer|min:1|max:300',
'NEW_LOG_LEVEL' => 'required|string',
'DEFAULT_AUTH_GATE' => 'required|string|in:liman,keycloak,ldap',
'JWT_TTL' => 'required|integer|min:15|max:999999',
], [], [
"EXTENSION_TIMEOUT" => "Eklenti zaman aşımı"
]);
Expand All @@ -61,6 +63,7 @@ public function saveConfiguration(Request $request)
'NEW_LOG_LEVEL' => $request->NEW_LOG_LEVEL,
'LDAP_IGNORE_CERT' => (bool) $request->LDAP_IGNORE_CERT,
'DEFAULT_AUTH_GATE' => $request->DEFAULT_AUTH_GATE,
'JWT_TTL' => $request->JWT_TTL,
]);

if ($request->has('LOGIN_IMAGE') && $request->LOGIN_IMAGE != '') {
Expand Down Expand Up @@ -89,6 +92,7 @@ public function saveConfiguration(Request $request)
'NEW_LOG_LEVEL' => $request->NEW_LOG_LEVEL,
'LDAP_IGNORE_CERT' => (bool) $request->LDAP_IGNORE_CERT,
'DEFAULT_AUTH_GATE' => $request->DEFAULT_AUTH_GATE,
'JWT_TTL' => $request->JWT_TTL,
],
"TWEAK_EDIT"
);
Expand Down
7 changes: 6 additions & 1 deletion app/Http/Controllers/API/Settings/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ public function update(Request $request)
'session_time' => ['required', 'integer', 'min:15', 'max:999999'],
]);

$session_time = env('JWT_TTL', 120);
if ($request->session_time == $session_time) {
$session_time = -1;
}

$data = [
'name' => $request->name,
'status' => $request->status,
'session_time' => $request->session_time,
'session_time' => $session_time,
];

if ($user->auth_type !== 'ldap') {
Expand Down
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ protected function google2faSecret(string $value = null): Attribute
);
}

/**
* Manipulate model's session time field
*/
public function getSessionTimeAttribute($value) {
if ($value == -1) {
return env('JWT_TTL', 120);
}

return $value;
}

/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('session_time')->default(-1)->change();

// Change all session_time values that is 120 to -1
DB::table('users')->where('session_time', 120)->update(['session_time' => -1]);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->bigInteger('session_time')->default(120)->change();

// Change all session_time values that is -1 to 120
DB::table('users')->where('session_time', -1)->update(['session_time' => 120]);
});
}
};

0 comments on commit 32b5c02

Please sign in to comment.