How to get guest user sessionid in Yii2? - yii2

I have used YII's session_id's to track user activity across a website with the following code snippet:
Yii::app()->getSession()->getSessionId()
How do I get same sessionid in Yii2? I tried quite possible way, but its all vain.
Please share the exact code snippet for Yii2.

You can try with
Yii::$app->session->getId();
this guide could be useful to you http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html
try checking if a session is already active before and ifnot use session open() .
session = Yii::$app->session;
// check if a session is already open
if ($session->isActive) ...
// open a session
$session->open();
// close a session
$session->close();
// destroys all data registered to a session.
$session->destroy();

$session = Yii::$app->session;
// if session is not open, open session
if ( !$session->isActive) { $session->open(); }
// get session id
Yii::$app->session->getId();
I got 26 character string in session id. "itddoa1lr247phpds34aemr0v0"
This link can be helpful: http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#sessions

Yii::$app->session->Id
And no need to explicitly open session. When you access session data through the session component, a session will be automatically opened if it has not been done so before.
http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#sessions

Related

How to forbid 2 users to edit the same data in a web application using MySQL (with CodeIgniter if possible)

I use CodeIgniter 3 and MariaDB 5.0 on my dev environment (but MySQL 5.6 in the prod environment).
I created a web app, and implemented a validation chain to accept the user requests.
For example:
User A asked for something
The manager validate
The deputy validate
The commission validate
The accounting department validate
The request is accepted
Those validations are registered in the database, in several tables.
I uses the SQL transaction to update/insert the data correctly with
CodeIgniter's functions and CRUD models, example:
$this->db->trans_start();
$this->my_model->create($data);
$another_data = $this->second_model->read($second_data);
$this->another_model->create($another_data);
$this->db->trans_complete();
But now, I have to implement a new rule for the General Manager. He has the capacity to validate anytime, and this validation breaks the validation chain because it count as everyone had validated.
This case could be an example:
User A asked for something
The manager validate
The general manager validate
The request is accepted
I'm not sure how to implement this, because I realised that 2 users (the general manager and the deputy for example) could consult the same request, one refuses, the other validate, and the data are not consistent anymore.
Is the transaction good enough ? Like:
// User A asked for something
// The manager validate
// The deputy consult the request
// The manager consult the request
// The deputy validate:
$this->db->trans_start();
// check if already validated by the general manager
if ($this->model->read($data)) {
$this->db->trans_complete();
// redirection to a warning page
}
else {
$this->my_model->create($my_data);
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
// redirection to another warning page
// The general manager wants to refuse at the same time but the deputy's transaction started first, so he's redirected to a warning page
$this->db->trans_start();
// check if already validated by anyone
if ($this->model->read($data)) {
$this->db->trans_complete();
// redirection to a warning page
}
else {
$this->my_model->create($my_data);
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
// redirection to another warning page
// The validation chain continues after the deputy's validation
I thought about the LOCK TABLE to forbid a user to reach the validation page while someone else is consulting the same request. But TRANSACTION and LOCK TABLE are hard to implement because it can trigger implicit commits, and I'm not used enough to do this right.
I read about START TRANSACTION READ WRITE that could be a better solution, but I didn't understood the all thing, and I'm not sure how to implement this correctly.
Could anyone help me to implement the good strategy, and if possible the way to use these SQL functions if needed ?
I found the solution, I used the FOR UPDATE just after my query, here it is:
// the query for my results
private model_function_for_query() {
$this->db->select('my_select');
$this->db->from('my_table');
$this->db->where('my_where_clause');
return $this->db->get_compiled_select();
}
// the function to apply the FOR UPDATE on the query
public model_function() {
$query = $this->model_function_for_query();
$result = $this->db->query($query.' FOR UPDATE');
return $result->row();
}
And in my controller:
// start the transaction
$this->db->trans_start();
// do the query, locking tables
$data = $this->my_model->model_function();
// check if the data has not changed
...
// if not, do the data treatment, else redirect to the warning page
...
// close the transaction
$this->db->trans_complete();
// check if the transaction failed, and do the stuff
if ($this->db->trans_status() === FALSE)
...
else
...

What does sessionStore.close() actually do in `express-mysql-session`?

It is not clear from documentation what this operation does.
Closing the session store
To cleanly close the session store:
sessionStore.close();
What does actually close the session store mean?
I need to sign out a user. I merely need to delete a session record from a DB. Do I need close the session store in order to achieve it?
In order to destroy a session
router.route('/logout')
.get((req, res, next) => {
req.session.destroy(function(err) {
return res.redirect(302, '/login');
})
});
From the source, calling sessionStore.close() ends the db connection.
To logout the user, you can either
destroy the session by calling req.session.destroy()
or, if you are using passport.js, call req.logOut()

Yii2 autologin with cookie

I've set up cookie based login just like the guy in this thread. I can see that auth key is always generated correctly. I can see it in the DB and in cookie also. Cookie also has the proper expiration date. Nevertheless, I never get signed in automatically. I have set 'enableAutoLogin' => true and all the other methods are as default. So it should work. I read somewhere that calling Yii::$app->user->identity should trigger cookie login, but it doesn't. Any advice?
Do you set duration for login method?
Method User::login() has second argument named "duration". By default it is 0. It mean the identity information will be stored in session as long as the session remains active. If you want autologin, set duration > 0.
The problem might be caused by data retrieved from cookie. The $id parameter in User::findIdentity was not a MongoId or string but an array. Simplest solution is to overwrite the method e.g:
public static function findIdentity($id)
{
if(!empty($id) && is_array($id))
$id = new \MongoId($id['$id']);
return static::findOne($id);
}

Username with System.User

Today I wanted to greet the user in my app by name, but I did not manage to get it.
I found System.User, but lacking some examples I did not manage to get the info I needed. I saw no possibility to get the current user (id) to call User.GetFromId().
Can you guide me into the right direction? Have I been on the wrong path?
Okay, So first things first, getting access to a user's personal information is a privilege you have to request, so in your store app's Package.appxmanifest, you'll need to enable the User Account Information capability in the Capabilities tab.
Next, you'll want to be using the Windows.System.User class, not the System.User (System.User isn't available to Windows store apps, which you appear to be discussing given the tags you provided for your question)
Third, you'll want to request personal information like this.
IReadOnlyList<User> users = await User.FindAllAsync(UserType.LocalUser, UserAuthenticationStatus.LocallyAuthenticated);
User user = users.FirstOrDefault();
if (user != null)
{
String[] desiredProperties = new String[]
{
KnownUserProperties.FirstName,
KnownUserProperties.LastName,
KnownUserProperties.ProviderName,
KnownUserProperties.AccountName,
KnownUserProperties.GuestHost,
KnownUserProperties.PrincipalName,
KnownUserProperties.DomainName,
KnownUserProperties.SessionInitiationProtocolUri,
};
IPropertySet values = await user.GetPropertiesAsync(desiredProperties);
foreach (String property in desiredProperties)
{
string result;
result = property + ": " + values[property] + "\n";
System.Diagnostics.Debug.WriteLine(result);
}
}
When you call GetPropertiesAsync, your user will get a permission prompt from the system asking them if they want to give you access to that. If they answer 'No', you'll get an empty user object (but you'll still get a unique token you can use to distinguish that user if they use the app again).
If they answer yes, you'll be able to get access to the properties below, and various others.
See the UserInfo Sample Microsoft provided for more examples.

Bolt: saveContent updates values but doesn't actually save the record

I'm trying to create new content with:
// Get suggestions template and update with values
$content = $this->app['storage']->getEmptyContent('suggestions');
$content->values['title'] = $title;
$content->values['description'] = $description;
// Save to db
$save = $this->app['storage']->saveContent($content);
status is set as publish in data returned from getEmptyContent.
When I visit the backend, I can see that the save status is None. How can I actually create it so that it is published?.
This sounds like it could be a bug since as far as I can remember some value should make it through to status by default. One thing to check, in your contenttypes.yml file for suggestions you can also add a default_status eg:
default_status: publish
If you still have no luck then raise an issue on Github.