29 votes
Accepted

How can I determine if a user is logged in, and who that user is?

To determine if the user is logged in at all, you'll want to use the currentUser variable: {% if currentUser %} Welcome, {{ currentUser.friendlyName }}! {% endif %} To determine if the logged in ...
user avatar
  • 23.6k
25 votes
Accepted

Check user is in User Group / Custom User Permissions

I believe what you're looking for is 'isInGroup' {% if currentUser.isInGroup('groupHandle') %} It will also accept an actual group object or a group ID as well. There is also getGroups: {{ ...
user avatar
  • 66.5k
11 votes

Check user is in User Group / Custom User Permissions

If you're looking to check groups and/or permissions from your plugin controller with PHP, you can access the same methods. Craft 3.0.x: //get the current user from craft $user = Craft::$app->...
user avatar
9 votes
Accepted

What's the difference between 'user', 'currentUser', and 'account'?

Update: As of Craft 3.x, the deprecated user variable no longer works. Make sure to use the proper currentUser variable instead! Original: user and currentUser are exactly the same. They contain the ...
user avatar
  • 23.6k
9 votes

Show content if logged in user is in userGroup1 OR userGroup2?

According to the docs, the userModel.isInGroup() method takes a "user group model, its handle, or ID" – in other words, it can only test for membership in a single group, not several. What you want ...
user avatar
6 votes
Accepted

Show content if logged in user is in userGroup1 OR userGroup2?

Going to chime in here as I ran into a similar issue, and solved it a bit differently. Instead of using group handles I was using a dynamic set of Group IDs. However, the problem still remains that ...
user avatar
  • 564
4 votes
Accepted

How do I relate a user to a channel entry from a form?

When saving an entry field type as part of your plug-in it needs to be an array of IDs in order to work correctly. This can either be handled via a plug-in or in the form by passing the value as an ...
user avatar
4 votes
Accepted

How can I interact with Users via the ElementAPI?

Change function (EntryModel $user) to function (UserModel $user) You were passing in a user to a function that’s expecting an EntryModel. That’s the reason for the error you are getting.
user avatar
  • 6,682
4 votes
Accepted

What does the Timezone in Settings -> General represent?

You set it to whatever timezone the content authors will be in, so for example, when they say an entry’s post date is at 10 AM, Craft knows which timezone that should refer to.
user avatar
4 votes

Users are always logged out on front end

I tracked down a similar problem, and documented it here: The Case of the Missing PHP Session I'm not sure if it'll address the issue you're having specifically, but it may give you some clue on ...
user avatar
4 votes

Change email address on front end

Thanks to billythekid's suggestion, adding a password field creates an elevated session, and solves my problem. <input type="password" name="password" placeholder="Password" required> Actually ...
user avatar
  • 864
3 votes

How do I get the current user’s info over Ajax?

For the redirect I agree, I could not get it too from ajax response, so I just saved it before the ajax call. var returnUrl = $("input[name='redirect']").val(); Then I can make the redirection : ....
user avatar
  • 972
3 votes
Accepted

Using the CP login session expiration for the frontend

What you want is this: // Returns how many seconds are left in the current user session. // (int) The seconds left in the session, or -1 if their session // will expire when their HTTP session ends. ...
user avatar
3 votes

conditional does not run first in a file that is included using the extends feature for twig

The issue here is that extends isn't the same thing, neither conceptually or technically, as an include. When you extend a template with a layout template, the template is "decorated" by the layout. ...
user avatar
  • 7,248
3 votes

CometChat integration

The Matt's answer was pretty complete and worked almost, but for the benefit of others, there is the changes required files to make it works: in integration.php find define('SET_SESSION_NAME',''); ...
user avatar
  • 147
2 votes

Check user is in User Group / Custom User Permissions

I had a similar situation: I wanted to restrict some users from certain entries. I've created a checkboxes field called "cf_userRights_checkboxes" where the label is the user-groups name and the ...
user avatar
  • 565
2 votes
Accepted

CometChat integration

So, the Comet Chat is not able to read the userid, even if I made a Craft plugin who writes UserID in $_SESSION['userid']. To access Craft's session variables outside Craft, you need to pull the ...
user avatar
2 votes
Accepted

How do I get the current user’s info over Ajax?

I found a solution using the Element API plugin, with the following endpoints config: <?php namespace Craft; return [ 'endpoints' => [ 'user/info.json' => [ '...
user avatar
2 votes

User specific entries (relations)

Instead of looping through currentMemberTest, you're currently setting up a fresh criteria model in your for loop. Use your nicely set currentMemberTest criteria! {% for entry in currentMemberTest %}...
user avatar
  • 35.9k
2 votes
Accepted

Variable for User Field

You could try using the attribute function in twig, this should let you call dynamic properties. Something like: {{ attribute(currentUser, question.fieldName) }}
user avatar
  • 4,519
2 votes
Accepted

How to compare currentUser with entry.author?

Try this: {% if currentUser.id == entry.author.id %} When using quotation marks, it means that it's a string in Twig. What you want to do is compare the value of the actual variable. Also, ...
user avatar
  • 7,248
2 votes
Accepted

user session for Multi Site

If both sites share a common root domain, you can share cookies by using that root domain as the cookie domain for both sites. This way, a login stored in the cookie for one site will also apply to ...
user avatar
  • 6,830
1 vote

Commerce: How can I check that a user has bought a product?

There is a direct way to find this out. Here's an example. Note the id you supply must be the specific purchasableId, not the product id (so the defaultVariant.id or variant.id). {# Has the customer ...
user avatar
1 vote

Craft 3 - Matrix Block for currentUser

Well, many tries later, I found a way to do it. {% for block in currentUser.certificado %} <div> <h3>{{ block.nomeDoCertificado }}</h3> <h3>{{ block.curso[0] }}</h3&...
user avatar
1 vote

Else statement outputting three bits of content

In Craft there are several so called global variables one of them is the currentUser thus you can check if someone is logged in with {% if currentUser %} {% else %} {% endif %} You don't need ...
user avatar
1 vote

List users connected to me

Solution: {% set myReverseConnections = craft.users.relatedTo({ targetElement: currentUser, field: 'yourCustomRelationshipField' }) %}
user avatar
1 vote
Accepted

Include user information in html block

There is a currentUser global variable that is available to all Twig templates. If there is a currently logged in user, it will return that user's UserModel class. From there, it's just a matter of: ...
user avatar
  • 66.5k
1 vote
Accepted

currentUser: Impossible to invoke a method ("isInGroup") on a null variable

Yes, that's expected behavior. currentUser will not exist if there is not a currently logged in user. {% if currentUser.isInGroup('verified') is defined %} That's one workaround for it. Another ...
user avatar
  • 66.5k
1 vote

Using the CP login session expiration for the frontend

I wanted to do something similar to this, but ran into the problem that if they were on another tab taking actions, they could extend the timeout, which would render the original set timeout to no ...
user avatar
1 vote
Accepted

sessions being read inconsistently

The site was on amazon AWS (without load balancing), so I set an appId in the config/general.php file to keep the session "sticky", even if the filepaths were "inconsistent" on a single server ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible