diff --git a/app/Halcyon/Access/Role.php b/app/Halcyon/Access/Role.php index 3b0b51b69..b328a0aab 100644 --- a/app/Halcyon/Access/Role.php +++ b/app/Halcyon/Access/Role.php @@ -16,6 +16,8 @@ * @property int $lft * @property int $rgt * @property string $title + * + * @property string $api */ class Role extends Model { diff --git a/app/Halcyon/Html/Builder/Access.php b/app/Halcyon/Html/Builder/Access.php index 2e8a175a0..bd5932b66 100644 --- a/app/Halcyon/Html/Builder/Access.php +++ b/app/Halcyon/Html/Builder/Access.php @@ -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; @@ -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 )); diff --git a/app/Modules/Storage/Http/Controllers/Admin/DirectoriesController.php b/app/Modules/Storage/Http/Controllers/Admin/DirectoriesController.php index 868bc7716..a6731a56f 100644 --- a/app/Modules/Storage/Http/Controllers/Admin/DirectoriesController.php +++ b/app/Modules/Storage/Http/Controllers/Admin/DirectoriesController.php @@ -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 diff --git a/app/Modules/Storage/Http/Controllers/Admin/NotificationTypesController.php b/app/Modules/Storage/Http/Controllers/Admin/NotificationTypesController.php index e3e8ebdc8..d6ed90342 100644 --- a/app/Modules/Storage/Http/Controllers/Admin/NotificationTypesController.php +++ b/app/Modules/Storage/Http/Controllers/Admin/NotificationTypesController.php @@ -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')); diff --git a/app/Modules/Storage/Http/Controllers/Admin/StorageController.php b/app/Modules/Storage/Http/Controllers/Admin/StorageController.php index 2fd278cf7..f10c80d54 100644 --- a/app/Modules/Storage/Http/Controllers/Admin/StorageController.php +++ b/app/Modules/Storage/Http/Controllers/Admin/StorageController.php @@ -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 ?: ''; diff --git a/app/Modules/Tags/Http/Controllers/Admin/TaggedController.php b/app/Modules/Tags/Http/Controllers/Admin/TaggedController.php index b32ef0f45..6fa24dd80 100644 --- a/app/Modules/Tags/Http/Controllers/Admin/TaggedController.php +++ b/app/Modules/Tags/Http/Controllers/Admin/TaggedController.php @@ -89,7 +89,7 @@ public function index(StatefulRequest $request) */ public function create() { - $row = new Tag(); + $row = new Tagged(); if ($fields = app('request')->old('fields')) { @@ -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); @@ -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')); @@ -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')) { @@ -181,7 +177,7 @@ public function delete(Request $request) foreach ($ids as $id) { - $row = Tag::findOrFail($id); + $row = Tagged::findOrFail($id); if (!$row->delete()) { diff --git a/app/Modules/Tags/Http/Controllers/Admin/TagsController.php b/app/Modules/Tags/Http/Controllers/Admin/TagsController.php index 26c63dbaf..cbb4849da 100644 --- a/app/Modules/Tags/Http/Controllers/Admin/TagsController.php +++ b/app/Modules/Tags/Http/Controllers/Admin/TagsController.php @@ -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) diff --git a/app/Modules/Themes/Http/Controllers/Admin/ThemesController.php b/app/Modules/Themes/Http/Controllers/Admin/ThemesController.php index 5a4fb4bf4..fecb9cb86 100644 --- a/app/Modules/Themes/Http/Controllers/Admin/ThemesController.php +++ b/app/Modules/Themes/Http/Controllers/Admin/ThemesController.php @@ -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()) diff --git a/app/Modules/Users/Events/UserLookup.php b/app/Modules/Users/Events/UserLookup.php index c14931ad8..a3545e5f6 100644 --- a/app/Modules/Users/Events/UserLookup.php +++ b/app/Modules/Users/Events/UserLookup.php @@ -6,7 +6,7 @@ class UserLookup { /** - * @var array + * @var array */ public $criteria; @@ -18,7 +18,7 @@ class UserLookup /** * Constructor * - * @param array $criteria + * @param array $criteria * @return void */ public function __construct(array $criteria) diff --git a/app/Modules/Users/Http/Controllers/Admin/LevelsController.php b/app/Modules/Users/Http/Controllers/Admin/LevelsController.php index bc6674c6a..b367c38e8 100644 --- a/app/Modules/Users/Http/Controllers/Admin/LevelsController.php +++ b/app/Modules/Users/Http/Controllers/Admin/LevelsController.php @@ -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); diff --git a/app/Modules/Users/Http/Controllers/Admin/NotesController.php b/app/Modules/Users/Http/Controllers/Admin/NotesController.php index 842dfcbba..29dd7fc1b 100644 --- a/app/Modules/Users/Http/Controllers/Admin/NotesController.php +++ b/app/Modules/Users/Http/Controllers/Admin/NotesController.php @@ -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' ]; @@ -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()) diff --git a/app/Modules/Users/Http/Controllers/Admin/RolesController.php b/app/Modules/Users/Http/Controllers/Admin/RolesController.php index ddbfc2e88..f3578131a 100644 --- a/app/Modules/Users/Http/Controllers/Admin/RolesController.php +++ b/app/Modules/Users/Http/Controllers/Admin/RolesController.php @@ -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); @@ -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 diff --git a/app/Modules/Users/Http/Controllers/Admin/UsersController.php b/app/Modules/Users/Http/Controllers/Admin/UsersController.php index 834259603..988bcddbb 100644 --- a/app/Modules/Users/Http/Controllers/Admin/UsersController.php +++ b/app/Modules/Users/Http/Controllers/Admin/UsersController.php @@ -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, @@ -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) @@ -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; diff --git a/app/Modules/Users/Http/Controllers/Api/LevelsController.php b/app/Modules/Users/Http/Controllers/Api/LevelsController.php index 3bc30a139..d808dd257 100644 --- a/app/Modules/Users/Http/Controllers/Api/LevelsController.php +++ b/app/Modules/Users/Http/Controllers/Api/LevelsController.php @@ -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()) { diff --git a/app/Modules/Users/Http/Controllers/Api/PermissionsController.php b/app/Modules/Users/Http/Controllers/Api/PermissionsController.php index def151de2..5fdc90bb4 100644 --- a/app/Modules/Users/Http/Controllers/Api/PermissionsController.php +++ b/app/Modules/Users/Http/Controllers/Api/PermissionsController.php @@ -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; /** @@ -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. @@ -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. diff --git a/app/Modules/Users/Http/Controllers/Api/UsersController.php b/app/Modules/Users/Http/Controllers/Api/UsersController.php index 9516b304c..2466c66d8 100644 --- a/app/Modules/Users/Http/Controllers/Api/UsersController.php +++ b/app/Modules/Users/Http/Controllers/Api/UsersController.php @@ -18,6 +18,7 @@ use App\Modules\Users\Events\UserDeleted; use App\Halcyon\Access\Map; use App\Halcyon\Access\Gate; +use Carbon\Carbon; /** * Users @@ -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) @@ -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', []); diff --git a/app/Modules/Users/Http/Controllers/Site/ForgotPasswordController.php b/app/Modules/Users/Http/Controllers/Site/ForgotPasswordController.php index 6bbac6446..8d6d9e666 100644 --- a/app/Modules/Users/Http/Controllers/Site/ForgotPasswordController.php +++ b/app/Modules/Users/Http/Controllers/Site/ForgotPasswordController.php @@ -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 { diff --git a/app/Modules/Users/Http/Controllers/Site/UsersController.php b/app/Modules/Users/Http/Controllers/Site/UsersController.php index 3fc495bb2..5242b5ce1 100644 --- a/app/Modules/Users/Http/Controllers/Site/UsersController.php +++ b/app/Modules/Users/Http/Controllers/Site/UsersController.php @@ -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()); diff --git a/app/Modules/Users/Http/Middleware/Blocked.php b/app/Modules/Users/Http/Middleware/Blocked.php index 076670d76..9cd915eae 100644 --- a/app/Modules/Users/Http/Middleware/Blocked.php +++ b/app/Modules/Users/Http/Middleware/Blocked.php @@ -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); } diff --git a/app/Modules/Users/Http/Middleware/LastActivity.php b/app/Modules/Users/Http/Middleware/LastActivity.php index 46710f98b..f0cb4d44b 100644 --- a/app/Modules/Users/Http/Middleware/LastActivity.php +++ b/app/Modules/Users/Http/Middleware/LastActivity.php @@ -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); diff --git a/app/Modules/Users/Models/Facet.php b/app/Modules/Users/Models/Facet.php index 44ba53705..5c150ba02 100755 --- a/app/Modules/Users/Models/Facet.php +++ b/app/Modules/Users/Models/Facet.php @@ -18,6 +18,8 @@ * @property string $value * @property int $locked * @property int $access + * + * @property string $api */ class Facet extends Model { diff --git a/app/Modules/Users/Models/Fields/AccessLevel.php b/app/Modules/Users/Models/Fields/AccessLevel.php index 23edb0fb9..010f6a4b5 100755 --- a/app/Modules/Users/Models/Fields/AccessLevel.php +++ b/app/Modules/Users/Models/Fields/AccessLevel.php @@ -3,6 +3,7 @@ use App\Halcyon\Access\Viewlevel; use App\Halcyon\Form\Fields\Select; +use stdClass; /** * Access level select @@ -19,26 +20,30 @@ class AccessLevel extends Select /** * Method to get the list of menus for the field options. * - * @return array The field option objects. + * @return array The field option objects. */ protected function getOptions() { - $all = new Viewlevel; + $all = new stdClass; $all->value = 0; $all->text = '- ' . trans('global.all') . ' -'; - $options = Viewlevel::query() + $options = array(); + $options[] = $all; + + $levels = Viewlevel::query() ->orderBy('id', 'asc') ->get(); - $options->each(function($item) + foreach ($levels as $level) { - $item->value = $item->id; - $item->text = $item->title; - }); + $option = new stdClass; + $option->value = $level->id; + $option->text = $level->title; - $options->prepend($all); + $options[] = $option; + } - return $options->toArray(); + return $options; } } diff --git a/app/Modules/Users/Models/Fields/Roles.php b/app/Modules/Users/Models/Fields/Roles.php index 877156f5b..aaaa7adef 100755 --- a/app/Modules/Users/Models/Fields/Roles.php +++ b/app/Modules/Users/Models/Fields/Roles.php @@ -4,6 +4,7 @@ use App\Halcyon\Access\Role; use App\Halcyon\Form\Fields\Select; use Illuminate\Support\Facades\DB; +use stdClass; /** * Form Field class @@ -20,15 +21,18 @@ class Roles extends Select /** * Method to get the list of menus for the field options. * - * @return array The field option objects. + * @return array The field option objects. */ protected function getOptions() { - $none = new Role; + $none = new stdClass; $none->value = 0; $none->text = trans('global.none'); - $options = Role::query() + $options = array(); + $options[] = $none; + + $roles = Role::query() ->select(['a.id', 'a.title', 'a.parent_id', DB::raw('COUNT(DISTINCT b.id) AS level')]) ->from($none->getTable() . ' AS a') ->leftJoin($none->getTable() . ' AS b', function ($join) @@ -40,14 +44,15 @@ protected function getOptions() ->orderBy('a.lft', 'asc') ->get(); - $options->each(function($item) + foreach ($roles as $role) { - $item->value = $item->id; - $item->text = str_repeat('|—', $item->level) . $item->title; - }); + $option = new stdClass; + $option->value = $role->id; + $option->text = str_repeat('|—', $role->level) . $role->title; - $options->prepend($none); + $options[] = $option; + } - return $options->toArray(); + return $options; } } diff --git a/app/Modules/Users/Models/Fields/Usergroup.php b/app/Modules/Users/Models/Fields/Usergroup.php index 0fa315a67..c33cdb446 100644 --- a/app/Modules/Users/Models/Fields/Usergroup.php +++ b/app/Modules/Users/Models/Fields/Usergroup.php @@ -38,31 +38,6 @@ protected function getInput() // Initialize JavaScript field attributes. $attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : ''; - // Iterate through the children and build an array of options. - foreach ($this->element->children() as $option) - { - - // Only add