28

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 is a specific user, then simply compare against their username: {% if currentUser and currentUser.username is 'johndoe' %} Hey Johnny Boy! {% endif %}...


20

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: {{ currentUser.getGroups() }} Will return an array of UserGroupModels all of the groups the user belongs to. And finally, can: {{ currentUser.can('permissionName') }} ...


9

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 UserModel of the currently logged in user. Why are they exactly the same? Originally, user was the preset value for the current user. However, there were many ...


9

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 in this case is the or operator: {% if currentUser and (currentUser.isInGroup('userGroup1') or currentUser.isInGroup('userGroup2')) %} {# secret content #} {...


8

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->getUser(); //check if the current user has permission to edit certain entries $user->getIdentity()->can('editEntries: 27') //check if user is in group $...


6

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 isInGroup() doesn't support an array of handles or ids. So you gotta run some checks. And, I think you can be little more dynamic using IDs. {% set ...


4

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.


4

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 array to that field handle. Plug-in: $user->setContentFromPost(array( 'entryField' => array(10) )); $success = craft()->users->saveUser($user); ...


4

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.


4

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 I don't think it's that intrusive, and it's a good security step that should generally be encouraged when making any profile edits.


3

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. So, what's happening is that the code in the layout is parsed after all of the code in the template. Check out this article on straightupcraft explains the ...


3

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. craft()->userSession->getAuthTimeout() You can then combine this with a Javascript setTimeout when the page loads so you can trigger whatever messages ...


3

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 where to look.


3

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 : .success(function(data) { if (data.success) { window.location.href = returnUrl; } For the user's data could you precise what you want to do?


3

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',''); change for define('SET_SESSION_NAME','CraftSessionId'); and make your getUserID functions look like function getUserID() { $userId = 0; // ...


2

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 session ID from the CraftSessionId cookie, and then use that to revive the session: $sessId = $_COOKIE[ 'CraftSessionId' ] ?: false; if ( $sessId ) { ...


2

I found a solution using the Element API plugin, with the following endpoints config: <?php namespace Craft; return [ 'endpoints' => [ 'user/info.json' => [ 'elementType' => 'User', 'criteria' => [ 'id' => craft()->userSession->getId() ], 'first' => true, ...


2

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 %} ... {% endfor %}


2

You could try using the attribute function in twig, this should let you call dynamic properties. Something like: {{ attribute(currentUser, question.fieldName) }}


2

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, currentUser is a UserModel, so you need to get the id property to compare it with entry.author.id.


1

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 explicitly bought the XXX product? Returns 0 (==false) or >= 1 (==true) #} {% set purchasedXXX = craft.orders() .user(currentUser) .hasPurchasables(...


1

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> <a href="{{ block.certificado[0].url }}" target="_blank">{{ block.nomeDoCertificado }}</a> </div> {% endfor %} It is as simple as ...


1

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 to loop a user array


1

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


1

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: {% if currentUser %} form=http://formsite.com/formname?email={{ currentUser.email }} {% endif %} Of course, what's inside the if block will look ...


1

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 would be: {%if currentUser is defined and currentUser.isInGroup('verified') %} which to me reads a little better even though it's more verbose.


1

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 longer be correct. To account for that, I created a controller in one of my plugins which would get the timeout for the user, and return that. public function ...


1

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 instance. The autogenerated appId based on filepath must have been interacting weird with Amazon's cloud setup. return array( '*' => array( 'appId' => '...


1

You can write a simple Craft plugin that listens for the onLogin event, like so: public function init() { craft()->on('userSession.onLogin', function(Event $event) { $username=$event->params['username']; $user = craft()->users->getUserByUsernameOrEmail($username); $_SESSION['userid'] = $user->id; }); } ...


1

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 value is the user-groups id. Then added that field to a section. In a section entry you will be able to set the user-groups that have permission and then check ...


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