Skip to content

Commit

Permalink
refactor: Simplify, check var existence
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Aug 10, 2023
1 parent d9b2a45 commit 7e4b1c3
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 100 deletions.
2 changes: 2 additions & 0 deletions app/Halcyon/Access/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* @property int $lft
* @property int $rgt
* @property string $title
*
* @property string $api
*/
class Role extends Model
{
Expand Down
8 changes: 5 additions & 3 deletions app/Halcyon/Html/Builder/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ public static function level($name, $selected, $attribs = '', $params = true, $i
* Displays a list of the available user groups.
*
* @param string $name The form field name.
* @param string $selected The name of the selected section.
* @param string|array $selected The name of the selected section.
* @param string $attribs Additional attributes to add to the select field.
* @param bool $allowAll True to add "All Groups" option.
* @param bool $allowAll True to add "All Groups" option.
* @param string $idtag
* @return string The required HTML for the SELECT tag.
*/
public static function usergroup($name, $selected, $attribs = '', $allowAll = true)
public static function usergroup($name, $selected, $attribs = '', $allowAll = true, $idtag = false)
{
$ug = new Role;

Expand Down Expand Up @@ -103,6 +104,7 @@ public static function usergroup($name, $selected, $attribs = '', $allowAll = tr
}

return Select::genericlist($options, $name, array(
'id' => $idtag,
'list.attr' => $attribs,
'list.select' => $selected
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function store(Request $request, $data = array(), $offset = 0): RedirectR
unset($data['bytesource']);
}

$row = $id ? Directory::findOrFail($id) : new Directory;
$row = Directory::findOrNew($id);

// Reset everything in case someone unchecked a box on the form
// Checked boxes will get set to 1 with the data fill below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function store(Request $request): RedirectResponse

$id = $request->input('id');

$row = $id ? Type::findOrFail($id) : new Type;
$row = Type::findOrNew($id);

$row->fill($request->input('fields'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function store(Request $request): RedirectResponse

$id = $request->input('id');

$row = $id ? StorageResource::findOrFail($id) : new StorageResource;
$row = StorageResource::findOrNew($id);

$row->fill($request->input('fields'));
$row->importhostname = $row->importhostname ?: '';
Expand Down
20 changes: 8 additions & 12 deletions app/Modules/Tags/Http/Controllers/Admin/TaggedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function index(StatefulRequest $request)
*/
public function create()
{
$row = new Tag();
$row = new Tagged();

if ($fields = app('request')->old('fields'))
{
Expand All @@ -110,7 +110,9 @@ public function create()
public function store(Request $request)
{
$rules = [
'fields.name' => 'required|string'
'fields.tag_id' => 'required|integer',
'fields.taggable_id' => 'required|integer',
'fields.taggable_type' => 'required|string|max:255'
];

$validator = Validator::make($request->all(), $rules);
Expand All @@ -124,20 +126,14 @@ public function store(Request $request)

$id = $request->input('id');

$row = $id ? Tag::findOrFail($id) : new Tag();
$row = Tagged::findOrNew($id);
$row->fill($request->input('fields'));
$row->slug = $row->normalize($row->name);

if (!$row->created_by)
if (!$row->created_by && auth()->user())
{
$row->created_by = auth()->user()->id;
}

if (!$row->updated_by)
{
$row->updated_by = auth()->user()->id;
}

if (!$row->save())
{
return redirect()->back()->withError(trans('global.messages.save failed'));
Expand All @@ -154,7 +150,7 @@ public function store(Request $request)
*/
public function edit($id)
{
$row = Tag::findOrFail($id);
$row = Tagged::findOrFail($id);

if ($fields = app('request')->old('fields'))
{
Expand All @@ -181,7 +177,7 @@ public function delete(Request $request)

foreach ($ids as $id)
{
$row = Tag::findOrFail($id);
$row = Tagged::findOrFail($id);

if (!$row->delete())
{
Expand Down
2 changes: 1 addition & 1 deletion app/Modules/Tags/Http/Controllers/Admin/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function store(Request $request)

$id = $request->input('id');

$row = $id ? Tag::findOrFail($id) : new Tag();
$row = Tag::findOrNew($id);
$row->fill($request->input('fields'));

if (!$row->created_by)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function store(Request $request)

$id = $request->input('id');

$row = $id ? Theme::findOrFail($id) : new Theme();
$row = Theme::findOrNew($id);
$row->fill($request->input('fields'));

if (!$row->save())
Expand Down
4 changes: 2 additions & 2 deletions app/Modules/Users/Events/UserLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class UserLookup
{
/**
* @var array<string,string>
* @var array<string,mixed>
*/
public $criteria;

Expand All @@ -18,7 +18,7 @@ class UserLookup
/**
* Constructor
*
* @param array<string,string> $criteria
* @param array<string,mixed> $criteria
* @return void
*/
public function __construct(array $criteria)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function store(Request $request)

$id = $request->input('id');

$row = $id ? Level::findOrFail($id) : new Level();
$row = Level::findOrNew($id);
$row->fill($request->input('fields'));
$row->rules = array_map('intval', $row->rules);

Expand Down
5 changes: 2 additions & 3 deletions app/Modules/Users/Http/Controllers/Admin/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function edit($id)
public function store(Request $request)
{
$rules = [
'fields.subject' => 'required|string',
//'fields.subject' => 'required|string',
'fields.body' => 'required|string'
];

Expand All @@ -164,8 +164,7 @@ public function store(Request $request)

$id = $request->input('id');

$row = $id ? Note::findOrFail($id) : new Note();

$row = Note::findOrNew($id);
$row->fill($request->input('fields'));

if (!$row->save())
Expand Down
5 changes: 3 additions & 2 deletions app/Modules/Users/Http/Controllers/Admin/RolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public function create()
public function store(Request $request)
{
$rules = [
'fields.title' => 'required|string|max:100'
'fields.title' => 'required|string|max:100',
'fields.parent_id' => 'required|integer|min:1'
];

$validator = Validator::make($request->all(), $rules);
Expand All @@ -105,7 +106,7 @@ public function store(Request $request)

$id = $request->input('id');

$row = $id ? Role::findOrFail($id) : new Role();
$row = Role::findOrNew($id);
$row->fill($request->input('fields'));

// Check the super admin permissions for group
Expand Down
6 changes: 3 additions & 3 deletions app/Modules/Users/Http/Controllers/Admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function index(StatefulRequest $request)

$rows = $query
->orderBy($filters['order'], $filters['order_dir'])
->paginate($filters['limit'], ['*'], 'page', $filters['page']);
->paginate((int)$filters['limit'], ['*'], 'page', (int)$filters['page']);

return view('users::admin.users.index', [
'rows' => $rows,
Expand Down Expand Up @@ -266,7 +266,7 @@ public function store(Request $request)
$id = $request->input('id');
$fields = $request->input('fields');

$user = $id ? User::findOrFail($id) : new User();
$user = User::findOrNew($id);
$user->fill($fields);

if (!$user->id)
Expand Down Expand Up @@ -498,7 +498,7 @@ public function disable(Request $request, $state = 0)
$user = User::findOrFail(intval($id));
$user->enabled = $state;

if (!$user->enabled && $user->id == auth()->user()->id)
if (!$user->enabled && auth()->user() && $user->id == auth()->user()->id)
{
$request->session()->flash('error', trans('users::users.error.cannot disable self'));
continue;
Expand Down
11 changes: 9 additions & 2 deletions app/Modules/Users/Http/Controllers/Api/LevelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,15 @@ public function update(Request $request, $id)
]);

$row = Level::findOrFail($id);
$row->fill($request->all());
$row->rules = array_map('intval', $row->rules);
if ($request->has('title'))
{
$row->title = $request->input('title');
}
if ($request->has('rules'))
{
$rules = $request->input('rules');
$row->rules = array_map('intval', $rules);
}

if (!$row->save())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Http\Resources\Json\ResourceCollection;
use App\Halcyon\Access\Viewlevel as Level;
use App\Halcyon\Access\Asset;
use App\Halcyon\Access\Rules;
use App\Halcyon\Models\Extension;

/**
Expand Down Expand Up @@ -191,7 +192,7 @@ public function update($module, Request $request)

if ($return instanceof \Exception)
{
return redirect()->back()->withInput()->withError($return->getMessage());
return response()->json(['message' => $return->getMessage()], 500);
}

// Check the validation results.
Expand All @@ -210,7 +211,7 @@ public function update($module, Request $request)
}
}

return redirect()->back()->withInput()->withErrors($errors);
return response()->json(['message' => $errors], 415);
}

// Save the rules.
Expand Down
7 changes: 4 additions & 3 deletions app/Modules/Users/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Modules\Users\Events\UserDeleted;
use App\Halcyon\Access\Map;
use App\Halcyon\Access\Gate;
use Carbon\Carbon;

/**
* Users
Expand Down Expand Up @@ -421,7 +422,7 @@ public function create(Request $request)
* "description": "Record not found"
* }
* }
* @param int $id
* @param int|string $id
* @return UserResource
*/
public function read(Request $request, $id)
Expand Down Expand Up @@ -586,12 +587,12 @@ public function update(Request $request, $id)
//$user->fill($fields);

// Can't block yourself
/*if ($user->block && $user->id == auth()->user()->id)
/*if ($user->block && auth()->user() && $user->id == auth()->user()->id)
{
throw new \Exception(trans('users::users.error.cannot block self'));
}*/

if ($request->has('roles') && auth()->user()->can('edit.state users'))
if ($request->has('roles') && auth()->user() && auth()->user()->can('edit.state users'))
{
$roles = $request->input('roles', []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
use App\Modules\Users\Models\User;

class ForgotPasswordController extends Controller
{
Expand Down
3 changes: 2 additions & 1 deletion app/Modules/Users/Http/Controllers/Site/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function account(Request $request)
);

// `segment()` is NOT zero-indexed. "account" will be segment 1
$i = array_search('account', $request->segments()) + 2;
$pos = array_search('account', $request->segments());
$i = $pos ? $pos + 2 : 0;

event($event = new UserDisplay($user, $request->segment($i)));
$sections = collect($event->getSections());
Expand Down
4 changes: 3 additions & 1 deletion app/Modules/Users/Http/Middleware/Blocked.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function __construct(Auth $auth)
*/
public function handle($request, Closure $next)
{
if ($this->auth->check() && $this->auth->user()->blocked)
if ($this->auth->check()
&& $this->auth->user()
&& $this->auth->user()->blocked)
{
abort(403);
}
Expand Down
14 changes: 9 additions & 5 deletions app/Modules/Users/Http/Middleware/LastActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ public function __construct(Auth $auth)
/**
* Handle an incoming request.
*
* @param equest $request
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($this->auth->check()
&& $this->auth->user()->last_visit < Carbon::now()->subMinutes(5)->toDateTimeString())
&& $this->auth->user())
{
$user = $this->auth->user();
//$user->update(['last_visit' => Carbon::now()->toDateTimeString()]);
$user->getUserUsername()->datelastseen = Carbon::now()->toDateTimeString();
$user->getUserUsername()->save();

if (!$user->last_visit || $user->last_visit->timestamp < Carbon::now()->subMinutes(5)->timestamp)
{
//$user->update(['last_visit' => Carbon::now()->toDateTimeString()]);
$user->getUserUsername()->datelastseen = Carbon::now();
$user->getUserUsername()->save();
}
}

return $next($request);
Expand Down
2 changes: 2 additions & 0 deletions app/Modules/Users/Models/Facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @property string $value
* @property int $locked
* @property int $access
*
* @property string $api
*/
class Facet extends Model
{
Expand Down
Loading

0 comments on commit 7e4b1c3

Please sign in to comment.