I'm trying to search in my encrypted model with mailisearch in Laravel.
I tried with database and mailisearch so far, but no success. I'm using default laravel encryption. Here is how I defined toSearchableArray:
public function toSearchableArray(): array
{
return [
'title' => $this->title,
'description' => Crypt::decryptString($this->description)
];
}
So in that case, when I try to import or search the data, it will return that Payload is invalid. Title field is working, cause it's not encrypted.
Any ideas how to fix that?
Actually, there is no need to decrypt it :D
It worked like 'description' => $this->description
Related
I use AuthComponent with CakePHP 3.8 and now I need to do some logic in Model buildRules method but for this I need to get the current user ID.
Is there any way to pass/retrieve it without using hacks such as accessing directly from the session.
I know that it is possible to pass id via validator from controller as described in CakePHP's documentation
https://book.cakephp.org/3/en/core-libraries/validation.html#using-custom-validation-rules
And it works for validation, however, I am unable to access the validator from the inside of build rules.
When I do as described in here, I get an empty object.
https://book.cakephp.org/3/en/orm/validation.html#using-validation-as-application-rules
It seems that I am able to attach new validation rules but unable to retrieve the "Passed" provider to get the User ID.
It seems a trivial thing but a I spent quite a few hours trying to get the id in a proper way.
OK, After working a bit more, I found how to retrieve user_id inside build rules. Might be helpful to someone.
Do this in the controller
$this->ExampleModel->validator('default')->provider('passed', [
'current_user' => $this->Auth->user('id')
]);
And then put this in you buildRules method
public function buildRules(RulesChecker $rules)
{
$user_id = $this->validator('default')->provider('passed')['current_user'];
$rules->add(
function ($entity, $options) use($user_id) {
//return boolean for pass or fail
},
'ruleName',
[
'errorField' => 'some_id',
'message' => 'Some ID field is inconsistent with App logic.'
]
);
return $rules;
}
The proper way to handle this is usually using either events, or saving options.
For example to make the ID available for the application rules of all tables, you could do something like this:
$this->getEventManager()->on('Model.beforeRules', function (
\Cake\Event\Event $event,
\Cake\Datasource\EntityInterface $entity,
\ArrayObject $options
) {
$options['current_user'] = $this->Auth->user('id');
});
It would be available in the $options argument of the rule accordingly, ie as $options['current_user'].
For a specific save operation you can pass it to the options of the save() call:
$this->ExampleModel->save($entity, [
'current_user' => $this->Auth->user('id')
]);
There's also plugins that can help you with it, for example muffin/footprint.
I have a large form (~150 inputs) and a classic Controller (with create/store methods). Also I'm using validation rules.
Store method:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|regex:/^[a-zA-Zа-яёА-ЯЁ]+$/u|min:2|max:50',
'phone' => 'required|digits_between:9,10',
'description' => 'required|min:20|max:500',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator);
}
$request['created_by'] = Auth::user()->id;
Profile::create($request->all());
return redirect()->route('admin.profile.index');
}
In this case, the form doesn't store any data...
When I tried to decrease input numbers (from 150 to 30) - data was stored successfully!
After that, I increase "php_memory_limit","max_input_vars" etc. but problem wasn't fixed.
Please help to find best way to store large forms.
UPD:
Form stores successfully without validation...
I tried to set just one simple rule - "required", but problem the problem has not disappeared
You can increase max_input_vars value in the php.ini file
or change your UI and split inputs on multiple pages
It's weird, but problem left when I changed
return redirect()->back()->withErrors($validator); to
return redirect()->back()->withErrors($validator->messages());
I'm currently working on application that will be storing data from many sources. The thing is that there will be 2-3 new sources each month, so I look for a way to allow users to add new keys within my application. The data will be highly sensitive, so I want to do it securely.
I don't want to enable putenv() function or read the .env file as a text.
Right now my #store action in the controller looks like that:
public function store(Request $request)
{
$this->validate($request, [
'customer_api' => 'required',
'secret_api' => 'required',
'edition_id' => 'required'
]);
$edition = $request->input('edition_id');
$secret = $request->input('secret_api');
$customer = $request->input('customer_api');
putenv("GF_SECRET_ED$edition=$secret");
putenv("GF_CUSTOMER_ED$edition=$customer");
return redirect('/editions/' . $edition . "#dev")->with('success', 'API keys added');
}
And I'm looking for a solution that will allow me to add those keys to .env, then to define those 3rd party keys in config/services.php
Couldn't find anything in docs. Working on Laravel 6.
Thank you in advance!
Modify env in your app dosen't right
you have two choices :
1- use database
2- using file and maybe with json format to easily retrieve data
I am really confused about my situation right now.
As you can see I have this controller in laravel that separates the data from a collection by filtering the collection
$bankedQuestions = $questions->whereNotIn('id', $sortedQuestionsID)->all();
$sortedQuestions = $questions->whereIn('id', $sortedQuestionsID)->all();
Everything works great and it both returns an array, checked it using dd(); then, I return it as a json
return response()->json([
'message' => 'Question loaded',
'sortedQuestions' => $sortedQuestions,
'questions' => $bankedQuestions,
'sortedIDs' => $sortedQuestionsID,
]);
THEN the confusing part comes into place. When I check the axios response the data types are very very different.
Any idea why this is happening guys? I've been checking the data and the parameters and everything are as expected. I'm using vueJS with Axios and Laravel.
Raw response:
Alright so I have an array of objects. If I do a print_r($arrObjects), I can see all of them in there looking good. Then I want to json_encode that for AngularJS to make a nice little $http.get request for me. However, before I can get to that though I tried serializing the array using the JsonSerializable interface but what happens after I do that then json_encode it I get an array of blank objects. This is not good.
Here is a pastebin of everything.
PastBinLink
I am sure this is something simple I am missing or messing up but I am out of ideas. Any help would be greatly appreciated. Thanks
Alright so I realized that I implemented the JsonSerializable on for handling the arrays but not for handling the actual class that I was using for all the objects. I implemented JsonSerializable then added:
public function jsonSerialize() {
return [
'title' => $this->title,
'image' => $this->image,
'description' => $this->description,
'url' => $this->url,
'video' => $this->video,
'source' => $this->source
];
}
After implementing the above I was able to get it to work just fine.