/** * Creates the form's admin Group. * * @param $project * @param $request * @return FormGroup */ private function makeAdminGroup(Form $form, Request $request) { $groupName = $form->name; $groupName .= ' Admin Group'; $adminGroup = new FormGroup(); $adminGroup->name = $groupName; $adminGroup->fid = $form->fid; $adminGroup->save(); $formProject = $form->project()->first(); $projectAdminGroup = $formProject->adminGroup()->first(); $projectAdmins = $projectAdminGroup->users()->get(); $idArray = []; foreach ($projectAdmins as $projectAdmin) { $idArray[] .= $projectAdmin->id; } if (!is_null($request['admins'])) { $idArray = array_unique(array_merge($request['admins'], $idArray)); } if (!empty($idArray)) { $adminGroup->users()->attach($idArray); } $adminGroup->create = 1; $adminGroup->edit = 1; $adminGroup->delete = 1; $adminGroup->ingest = 1; $adminGroup->modify = 1; $adminGroup->destroy = 1; $adminGroup->save(); return $adminGroup; }
public function restoreData(Request $request) { $this->json_file = null; $this->decoded_json = null; $users_exempt_from_lockout = new Collection(); $users_exempt_from_lockout->put(1, 1); //Add another one of these with (userid,userid) to exempt extra users $this->lockUsers($users_exempt_from_lockout); if ($request->session()->has("restore_file_path")) { $filepath = $request->session()->get("restore_file_path"); $request->session()->forget("restore_file_path"); } else { return $this->ajaxResponse(false, "You did not select a valid restore point or upload a valid backup file"); } try { $this->json_file = Storage::get($filepath); } catch (\Exception $e) { $this->ajaxResponse(false, "The backup file couldn't be opened. Make sure it still exists and the permissions are correct."); } try { $this->decoded_json = json_decode($this->json_file); $this->decoded_json->kora3; } catch (\Exception $e) { $this->ajaxResponse(false, "The backup file contains invalid JSON data, it may be corrupt or damaged. Check the file or try another one.\n The restore did not start, so data already in the database was not deleted."); } $backup_data = $this->decoded_json; //Delete all existing data try { foreach (User::all() as $User) { if ($User->id == 1) { //Do not delete the default admin user continue; } else { $User->delete(); } } foreach (Project::all() as $Project) { $Project->delete(); } foreach (Form::all() as $Form) { $Form->delete(); } foreach (Field::all() as $Field) { $Field->delete(); } foreach (Record::all() as $Record) { $Record->delete(); } foreach (Metadata::all() as $Metadata) { $Metadata->delete(); } foreach (Token::all() as $Token) { $Token->delete(); } foreach (Revision::all() as $Revision) { $Revision->delete(); } foreach (DateField::all() as $DateField) { $DateField->delete(); } foreach (FormGroup::all() as $FormGroup) { $FormGroup->delete(); } foreach (GeneratedListField::all() as $GeneratedListField) { $GeneratedListField->delete(); } foreach (ListField::all() as $ListField) { $ListField->delete(); } foreach (MultiSelectListField::all() as $MultiSelectListField) { $MultiSelectListField->delete(); } foreach (NumberField::all() as $NumberField) { $NumberField->delete(); } foreach (ProjectGroup::all() as $ProjectGroup) { $ProjectGroup->delete(); } foreach (RichTextField::all() as $RichTextField) { $RichTextField->delete(); } foreach (ScheduleField::all() as $ScheduleField) { $ScheduleField->delete(); } foreach (TextField::all() as $TextField) { $TextField->delete(); } } catch (\Exception $e) { $this->ajaxResponse(false, "There was a problem when attempting to remove existing information from the\n database, the database user may not have permission to do this or the database may be in use."); } try { //This try-catch is for non-QueryExceptions, like if a table is missing entirely from the JSON data // User foreach ($backup_data->users as $user) { try { $new_user = User::create(array("username" => $user->username, "name" => $user->name, "email" => $user->email, "password" => $user->password, "organization" => $user->organization, "language" => $user->language, "regtoken" => $user->regtoken)); $new_user->id = $user->id; $new_user->admin = $user->admin; $new_user->active = $user->active; $new_user->remember_token = $user->remember_token; $new_user->created_at = $user->created_at; $new_user->updated_at = $user->updated_at; $new_user->locked_out = true; $new_user->save(); } catch (\Exception $e) { $this->ajax_error_list->push($e->getMessage()); } } // Project foreach ($backup_data->projects as $project) { //$new_project = Project::create(array("name" => $project->name, "slug" => $project->slug, "description" => $project->description, "adminGID" => $project->adminGID, "active" => $project->active)); try { $new_project = Project::create(array()); $new_project->name = $project->name; $new_project->slug = $project->slug; $new_project->description = $project->description; $new_project->adminGID = $project->adminGID; $new_project->active = $project->active; $new_project->pid = $project->pid; $new_project->created_at = $project->created_at; $new_project->updated_at = $project->updated_at; $new_project->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Form foreach ($backup_data->forms as $form) { try { $new_form = Form::create(array("pid" => $form->pid)); $new_form->fid = $form->fid; $new_form->name = $form->name; $new_form->slug = $form->slug; $new_form->description = $form->description; $new_form->adminGID = $form->adminGID; $new_form->layout = $form->layout; $new_form->public_metadata = $form->public_metadata; $new_form->layout = $form->layout; $new_form->adminGID = $form->adminGID; $new_form->created_at = $form->created_at; $new_form->updated_at = $form->updated_at; $new_form->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Field foreach ($backup_data->fields as $field) { try { $new_field = Field::create(array("pid" => $field->pid, "fid" => $field->fid, "order" => $field->order, "type" => $field->type, "name" => $field->name, "slug" => $field->slug, "desc" => $field->desc, "required" => $field->required, "default" => $field->default, "options" => $field->options)); $new_field->flid = $field->flid; $new_field->default = $field->default; $new_field->options = $field->options; $new_field->created_at = $field->created_at; $new_field->updated_at = $field->updated_at; $new_field->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // FormGroup foreach ($backup_data->formgroups as $formgroup) { try { $new_formgroup = new FormGroup(); $new_formgroup->name = $formgroup->group_data->name; $new_formgroup->fid = $formgroup->group_data->fid; $new_formgroup->create = $formgroup->group_data->create; $new_formgroup->edit = $formgroup->group_data->edit; $new_formgroup->ingest = $formgroup->group_data->ingest; $new_formgroup->delete = $formgroup->group_data->delete; $new_formgroup->modify = $formgroup->group_data->modify; $new_formgroup->destroy = $formgroup->group_data->destroy; $new_formgroup->id = $formgroup->group_data->id; $new_formgroup->created_at = $formgroup->group_data->created_at; $new_formgroup->updated_at = $formgroup->group_data->updated_at; $new_formgroup->save(); foreach ($formgroup->user_data as $user_id) { $new_formgroup->users()->attach($user_id); } } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // ProjectGroup foreach ($backup_data->projectgroups as $projectgroup) { try { $new_projectgroup = new ProjectGroup(); $new_projectgroup->id = $projectgroup->group_data->id; $new_projectgroup->name = $projectgroup->group_data->name; $new_projectgroup->pid = $projectgroup->group_data->pid; $new_projectgroup->create = $projectgroup->group_data->create; $new_projectgroup->edit = $projectgroup->group_data->edit; $new_projectgroup->delete = $projectgroup->group_data->delete; $new_projectgroup->created_at = $projectgroup->group_data->created_at; $new_projectgroup->updated_at = $projectgroup->group_data->updated_at; $new_projectgroup->save(); foreach ($projectgroup->user_data as $user_id) { $new_projectgroup->users()->attach($user_id); } } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Record foreach ($backup_data->records as $record) { try { $new_record = new Record(array("pid" => $record->pid, "fid" => $record->fid, "owner" => $record->owner, "kid" => $record->kid)); $new_record->rid = $record->rid; $new_record->created_at = $record->created_at; $new_record->updated_at = $record->updated_at; $new_record->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // TextField foreach ($backup_data->textfields as $textfield) { try { $new_textfield = new TextField(array("rid" => $textfield->rid, "flid" => $textfield->flid, "text" => $textfield->text)); $new_textfield->id = $textfield->id; $new_textfield->created_at = $textfield->created_at; $new_textfield->updated_at = $textfield->updated_at; $new_textfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // RichTextField foreach ($backup_data->richtextfields as $richtextfield) { try { $new_richtextfield = new RichTextField(array("rid" => $richtextfield->rid, "flid" => $richtextfield->flid, "rawtext" => $richtextfield->rawtext)); $new_richtextfield->id = $richtextfield->id; $new_richtextfield->created_at = $richtextfield->created_at; $new_richtextfield->updated_at = $richtextfield->updated_at; $new_richtextfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // NumberField foreach ($backup_data->numberfields as $numberfield) { try { $new_numberfield = new NumberField(array("rid" => $numberfield->rid, "flid" => $numberfield->flid, "number" => $numberfield->number)); $new_numberfield->id = $numberfield->id; $new_numberfield->created_at = $numberfield->created_at; $new_numberfield->updated_at = $numberfield->updated_at; $new_numberfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // ListField foreach ($backup_data->listfields as $listfield) { try { $new_listfield = new ListField(array("rid" => $listfield->rid, "flid" => $listfield->flid, "option" => $listfield->option)); $new_listfield->id = $listfield->id; $new_listfield->created_at = $listfield->created_at; $new_listfield->updated_at = $listfield->updated_at; $new_listfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // GeneratedListField foreach ($backup_data->generatedlistfields as $generatedlistfield) { try { $new_generatedlistfield = new GeneratedListField(array("rid" => $generatedlistfield->rid, "flid" => $generatedlistfield->flid, "options" => $generatedlistfield->options)); $new_generatedlistfield->id = $generatedlistfield->id; $new_generatedlistfield->created_at = $generatedlistfield->created_at; $new_generatedlistfield->updated_at = $generatedlistfield->updated_at; $new_generatedlistfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // MultiSelectListField foreach ($backup_data->multiselectlistfields as $multiselectlistfield) { try { $new_multiselectlistfield = new MultiSelectListField(array("rid" => $multiselectlistfield->rid, "flid" => $multiselectlistfield->flid, "options" => $multiselectlistfield->options)); $new_multiselectlistfield->id = $multiselectlistfield->id; $new_multiselectlistfield->created_at = $multiselectlistfield->created_at; $new_multiselectlistfield->updated_at = $multiselectlistfield->updated_at; $new_multiselectlistfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // DateField foreach ($backup_data->datefields as $datefield) { try { $new_datefield = new DateField(); $new_datefield->id = $datefield->id; $new_datefield->rid = $datefield->rid; $new_datefield->flid = $datefield->flid; $new_datefield->circa = $datefield->circa; $new_datefield->month = $datefield->month; $new_datefield->day = $datefield->day; $new_datefield->year = $datefield->year; $new_datefield->era = $datefield->era; $new_datefield->created_at = $datefield->created_at; $new_datefield->updated_at = $datefield->updated_at; $new_datefield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // ScheduleField foreach ($backup_data->schedulefields as $schedulefield) { try { $new_schedulefield = new ScheduleField(array("rid" => $schedulefield->rid, "flid" => $schedulefield->flid, "events" => $schedulefield->events)); $new_schedulefield->id = $schedulefield->id; $new_schedulefield->created_at = $schedulefield->created_at; $new_schedulefield->updated_at = $schedulefield->updated_at; $new_schedulefield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // GeolocatorField foreach ($backup_data->geolocatorfields as $geolocatorfield) { try { $new_geolocatorfield = new GeolocatorField(array("rid" => $geolocatorfield->rid, "flid" => $geolocatorfield->flid, "locations" => $geolocatorfield->locations)); $new_geolocatorfield->id = $geolocatorfield->id; $new_geolocatorfield->created_at = $geolocatorfield->created_at; $new_geolocatorfield->updated_at = $new_geolocatorfield->updated_at; $new_geolocatorfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Token foreach ($backup_data->tokens as $token) { try { $new_token = new Token(array('token' => $token->token, 'type' => $token->type)); $new_token->id = $token->id; $new_token->created_at = $token->created_at; $new_token->updated_at = $token->updated_at; $new_token->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Metadata foreach ($backup_data->metadatas as $metadata) { try { $new_metadata = new Metadata(array()); $new_metadata->flid = $metadata->flid; $new_metadata->pid = $metadata->pid; $new_metadata->fid = $metadata->fid; $new_metadata->name = $metadata->name; $new_metadata->created_at = $metadata->created_at; $new_metadata->updated_at = $metadata->updated_at; $new_metadata->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Revision foreach ($backup_data->revisions as $revision) { try { $new_revision = new Revision(array('id' => $revision->id, 'fid' => $revision->fid, 'rid' => $revision->rid, 'userId' => $revision->userId, 'type' => $revision->type, 'data' => $revision->data, 'oldData' => $revision->oldData, 'rollback' => $revision->rollback)); $new_revision->created_at = $revision->created_at; $new_revision->updated_at = $revision->updated_at; $new_revision->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } } catch (\Exception $e) { $this->ajax_error_list->push($e->getMessage()); $this->ajaxResponse(false, "An unknown error prevented the restore from completing.\n You can try restoring from a different backup file or restore point.\n Users will stay locked out until you run a successful restore or manually unlock them above.\n For this error, it's not recommended that you unlock users unless you have resolved the problem"); } if (count($this->ajax_error_list) != 0) { $this->ajaxResponse(false, "Not all of your data was restored, check the errors below for details.\n The errors are in order that they occurred, if you can resolve the first error, it will often correct\n one or more of the errors below it.\n Users will stay locked out until you run a successful restore or manually unlock them above."); } else { $this->unlockUsers(); $this->ajaxResponse(true, "The restore completed successfully."); } }