Welcome Guest, Not a member yet? Register   Sign In
Myth:Auth
#41

(This post was last modified: 11-15-2020, 01:54 PM by InsiteFX. Edit Reason: updated with error checking on user id )

I put this together tonight may help you.

PHP Code:
    // -------------------------------------------------------------------

    /**
     * getUser ()
     * -------------------------------------------------------------------
     *
     * Usage:
     * NOTE: $id can be left blank it will then user the auth_helper
     *       user_id() method. But this only works if user is logged in.
     *
     * $user = $this->getUser($id);
     * $user = $this->getUser();
     *
     * echo $user->username;
     *
     * @param  int $id
     * @return object
     */
    
public function getUser(int $id 0) : object
    
{
        
$data = [];

        
// connect to the database.
        
$db db_connect();
        
$builder $db->table('users');

        
/**
         * check user id if it does not exsist we assign the admin id
         * to avoid error.
         */
        
if ($id == 0)
        {
            
// get the logged in user
            
if (user_id() != null)
            {
                
$id user_id();
            }
            else
            {
                
$id 1;
            }
        }

        
// get the users record.
        
$query $builder->where('id'$id)
        
                 ->get();

        
$results $query->getResult();

        foreach (
$results as $row)
        {
            
$data $row;
        }

        
$query->freeResult();

        return 
$data;
    } 

Returns the complete user object.

10-15-2020 - Update to do better error checking on the user id.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#42

@InsiteFX
Perfect, thanks Smile
Reply
#43

You can change the result to an array if you like, I like to work with objects.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#44

Almost there, one thing though...

I'm trying to create a form where administrators can create new users. It has a list of groups that can be assigned to the new user. By reading Myth's code I figured out how to create the user in my own controller and how to assign groups to user id with GroupModel. My problem is, I don't know the user id of the newly created user.

Is there some direct way to find out what the user id is?

Code:
$users = model('UserModel');
...
$user = new User($this->request->getPost($allowedPostFields));
$users->save($user);

// how do i get the id of the newly created $user?
Reply
#45

You need to return the insert id of the new user, you could also pass it in the session.

PHP Code:
$db->insertID() 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#46

@InsiteFX
Thanks.
Much simpler than the custom User model and entity I wrote to get around the problem Big Grin
Reply
#47

Nice work and nice package!

I have question

How to extends user model & user entities ?. I get this error when try to load getDisplayName() function on Entities

Quote:ErrorException #1

Class 'Myth\Auth\Entities\User' not found




app/Entities/User.php


PHP Code:
<?php namespace App\Entities;

class 
User extends \Myth\Auth\Entities\User
{
    public function 
getDisplayName()
    {
        return 
'test';
    }


app/Models/UserModel.php

PHP Code:
<?php namespace App\Models;

class 
UserModel extends \Myth\Auth\Models\UserModel
{
protected 
$returnType 'App\Entities\User';
    protected $allowedFields = [
        'email''username''password_hash''reset_hash''reset_at''reset_expires''activate_hash',
        'status''status_message''active''force_pass_reset''permissions''deleted_at',
        'firstname''lastname''phone',
    ];


Thanks
Reply
#48

“ Class 'Myth\Auth\Entities\User' not found”

Sounds like you don’t have the package installed correctly. Are you able to use other portions of it?
Reply
#49

(12-06-2020, 05:33 AM)MGatner Wrote: “ Class 'Myth\Auth\Entities\User' not found”

Sounds like you don’t have the package installed correctly. Are you able to use other portions of it?


Nevermind m8, i found this issue. I need to load function authentication on Services and all is working good. good reference for extending entities and so on . i take it from : https://github.com/lonnieezell/myth-forums
Reply
#50

(This post was last modified: 12-30-2020, 06:30 AM by suvi.)

In Config/Auth.php theres this:

PHP Code:
    //--------------------------------------------------------------------
    // Allow Invitations
    //--------------------------------------------------------------------
    // When enabled (default) users may invite unregistered users to
    // create an account and collaborate on a specific job. This will
    // generate a tokenized URL to send to the specified email that will
    // prompt the user to make an account.
    //
    public $allowInvitations true

How can I generate the tokenized URL?
Didn't find anything in the docs or the source code.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB