1084

Members module: how do I provide access to a user with trongate_user_levels = 3?

Comments for “Members module: how do I provide access to a user with trongate_user_levels...”
 

Posted by mjim on Tuesday 20th February 2024 at 23:50 GMT

I can see in the docs that this will work to check for a valid token if they are a user with level 3 or 4:

$this->module('trongate_tokens');
$token = $this->trongate_tokens->_attempt_get_valid_token([3,4]);
if ($token == false) {
  echo 'You are not allowed to access this page';
} else {
  echo 'This is private content';
}


But that is not practical when I am using the members module.

How can I use the Members module to provide access to a module for users with trongate_user_levels = 3 OR trongate_user_levels = 4?

This comment was edited by mjim on Tuesday 20th February 2024 at 23:52 GMT

Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

Posted by djnordeen on Wednesday 21st February 2024 at 16:22 GMT

Hello,
function _make_sure_allowed_3() {
// Check if the user is logged in
$this->module('trongate_tokens');
$token = $this->trongate_tokens->_attempt_get_valid_token();
$member_obj = $this->_get_member_obj($token);
use an if statement to determine if there allowed.
trongate_user_id == 3
This produces the following
{
"trongate_user_code":"dytcGEc47h6VCmYmrJLwH5CPx8GMWcKa",
"user_level_id":"1",
"user_level":"admin",
"token":"r8Ex_25FoYFSsrJTn3wezn6wzW8XRLkf",
"trongate_user_id":"1",
"expiry_date":"1629508409"
}

Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by mjim on Thursday 22nd February 2024 at 05:47 GMT

Thank you for posting a solution Dan.

My revised code below worked and was actually very simple.

Here is how I provide access to both the admin and the user level 3 (manager) to pages within the managers module:

<?php
class Managers extends Trongate {

  function index() {
    $token = $this->_make_sure_allowed();
    // json($token);
    $this->module('members');
    $member_obj = $this->members->_get_member_obj($token);
    $data = (array) $member_obj;
    json($data, true);
  }

  function _make_sure_allowed() {
    $this->module('trongate_tokens');
    $token = $this->trongate_tokens->_attempt_get_valid_token([1,3]);
    if ($token == false) {
      // redirect to account
      redirect('members-account/your_account');
    } else {
      echo 'You have access to this private content';
      return $token;
    }
    
  }

}
Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

Posted by djnordeen on Thursday 22nd February 2024 at 12:58 GMT

Glad I could help
Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

×