CI 묻고 답하기

제목 php 레거시 -> ci4 중 shield에 관한 질문입니다.
카테고리 CI 4 관련
글쓴이 ci4어린이 작성시각 2023/10/06 13:25:37
댓글 : 2 추천 : 0 스크랩 : 0 조회수 : 2620   RSS

기존 php 5.2정도로 만들어진 프로젝트를 ci4로 새로 리뉴얼 하며 작업하고 있습니다.

DB와 호환되는 라이브러리들은 기존의 것을 가져다 사용하려 합니다.

ci4쪽으로 진행하다보니 사용자관리및 보안 라이브러리로 ci4 shield라는 패키지 가 있어 적용하려 하는데

Vender를 수정하지 않고 기존 DB에서 도출한 회원모델, 기능들과 shield의 모델및 기능들을 연결하여 사용할 수 있을까요?

ex)


<?php

declare(strict_types=1);

namespace App\Models;

use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;

class UserModel extends ShieldUserModel
{
    protected function initialize(): void
    {
        parent::initialize();
        $this->table = 'member';
        $this->returnType = 'object';
        $this->primaryKey = 'id';
        $this->allowedFields = [
            ...$this->allowedFields,
            'id', 'pass', 'state','mem_type','cno','name','zip',
            'addr1','addr2','email','phone','cphone','fax','boss_name',
            'admin_name','channel','server_id','cash','reg_date'
            // 'first_name',
        ];
    }
}

이런식으로 shield의 모델을 extends하여 사용하여야 할까요?

국내/외 포럼및 글들을 읽어보아도 기존 프로젝트에 shield적용하는 글이 없어서 문의 남깁니다.

감사합니다.

태그 shield,db,ci4
 다음글 여러 파일 업로드 시 validation 적용하는 문제 (1)
 이전글 ci3 업로드 설정 파일 적용 관련 (1)

댓글

변종원(웅파) / 2023/10/10 11:39:28 / 추천 0
composer로 설치되는 것이면 설치후 shield의 매뉴얼대로 작업하시면 됩니다.
ci4어린이 / 2023/10/10 15:48:26 / 추천 0

ㄴ변종원(웅파)

넵 메뉴얼대로 진행하고는 싶은데 기존 프로젝트에서 리뉴얼 하다보니 이미 사용하던 DB와, 외부 암호화 라이브러리에 연결하는 부분에 대한 설명이 따로 없더군요. 20년 이상된 프로젝트다 보니 db쪽의 data가 상당히 많기도 하고, oauth2를 추가하려다 보니 shield를 사용하여 사용자 인증/ 인가에 대한부분을 구성해놓는게 좋겠다 싶어 이용하려합니다.

VENDER파일은 수정하는게 아니라고 해서 최대한 상속하여 기능처리를 진행중인데 이렇게 하는 방법이 맞나 싶네요.

하단 내용은 수정중인 코드입니다

<?php

declare(strict_types=1);

namespace App\Models;

use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;

class UserModel extends ShieldUserModel
{
    protected $useSoftDeletes = false;
    protected function initialize(): void
    {
        parent::initialize();
        $this->table = 'member';
        $this->returnType = 'object';
        $this->primaryKey = 'id';
        
        $this->allowedFields = [
            'id', 'pass', 'state','mem_type','cno','name','zip',
            'addr1','addr2','email','phone','cphone','fax','boss_name',
            'admin_name','channel','server_id','cash','reg_date'
        ];
    }

    public function findByCredentials(array $credentials)
    {
        // Email is stored in an identity so remove that here
        $username = $credentials['id'] ?? null;
        unset($credentials['id']);

        if ($username === null && $credentials === []) {
            return null;
        }

        // any of the credentials used should be case-insensitive
        foreach ($credentials as $key => $value) {
            $this->where(
                'LOWER(' . $this->db->protectIdentifiers($this->table . ".{$key}") . ')',
                strtolower($value)
            );
        }
        //sprintf('%1$s.id as username, %1$s.pass as pass', $this->table)
        //->join($this->tables['identities'], sprintf('%1$s.user_id = %2$s.id', $this->tables['identities'], $this->table))
        
        if ($username !== null) {
            $data = $this->select(
                sprintf('%1$s.id as username, %1$s.pass as pass', $this->table)
            )
                ->where(
                    'LOWER(' . $this->db->protectIdentifiers($this->table . '.username') . ')',
                    strtolower($username)
                )
                ->asArray()
                ->first();

            if ($data === null) {
                return null;
            }

            $username = $data['username'];
            unset($data['username']);
            $pass = $data['pass'];
            unset($data['pass']);

            $user                = new User($data);
            $user->username         = $username;
            $user->pass = $pass;
            $user->syncOriginal();

            return $user;
        }

        return $this->first();
    }
}