API는 로그인한 사용자, 또는 비로그인한 사용자에 따라서 다르게 보여줘야합니다.
관리자가 아니라면 $config 변수의 키값들이라던가, 각각의 게시글 또는 회원의 패스워드를 노출하지 않아야 하고,
그 외의 접근권한이 없는 회원들에게는 아예 노출해야 하지 않아야 하는 경우도 있습니다.
그누보드내에서 이러한 해당 권한 변수 중 가장 대표적인게
$is_admin, $is_guest, $is_member, $member 인데
해당 부분은 매 api 접근시마다 체크해주시는게 좋습니다.
php를 예로 들면
[code]
public function __construct() {
public $g5;
public $dsn = "mysql:host=".G5_MYSQL_HOST.";port=3306;dbname=".G5_MYSQL_DB.";charset=utf8";
public $db;
public $is_member = false;
public $is_guest = false;
public $is_admin = '';
public $board = array();
public $config = array();
public $group = array();
public $member = array();
private $key = 'haskdlfjoieqimfqeif';
private $cookiename = 'gnu_jwt';
public function __construct() {
try {
$this->db = new PDO($this->dsn, G5_MYSQL_USER, G5_MYSQL_PASSWORD);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->init();
} catch(PDOException $e) {
echo $this->msg('DB 연결에 실패하였습니다');
$e->getMessage();
}
}
public function sql_query($query, $condition=array()) {
try {
$stmt = $this->db->prepare($query);
if($condition) $stmt->execute($condition);
else $stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
} catch(PDOException $e) {
return $e->getMessage();
}
}
public function sql_fetch($query, $condition=array()) {
try {
$stmt = $this->db->prepare($query);
if($condition) $stmt->execute($condition);
else $stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
return $e->getMessage();
}
}
// 관리자인가?
public function is_admin($mb_id) {
if (!$mb_id) return '';
$is_authority = '';
if ($this->config['cf_admin'] == $mb_id){
$is_authority = 'super';
} else if (isset($this->$group['gr_admin']) && ($this->$group['gr_admin'] == $mb_id)){
$is_authority = 'group';
} else if (isset($this->$board['bo_admin']) && ($this->$board['bo_admin'] == $mb_id)){
$is_authority = 'board';
}
return $is_authority;
}
//권한체크
public function init() {
global $g5;
$config = $this->sql_fetch("SELECT * FROM {$g5['config_table']}"); //그누보드 설정
if(isset($_COOKIE[$this->cookiename])) {
$decoded = JWT::decode($_COOKIE[$this->cookiename], $this->key, array('HS256')); //로그인 여부
$mb_id = $decoded->aud;
$this->member = $this->sql_fetch("SELECT * FROM {$g5['member_table']} WHERE mb_id = ?", [$mb_id]); //회원정보 설정
$this->is_admin = $this->is_admin($member['mb_id']);
$this->is_member = true;
$this->is_guest = false;
}else {
$this->is_guest = true;
$this->is_admin = false;
$this->is_member = false;
$this->member['mb_id'] = '';
$this->member['mb_level'] = 1; // 비회원의 경우 회원레벨을 가장 낮게 설정
}
}
[/code]
로 해당 api 접근시 기본적인 권한에 대해 가져오고 있습니다.
아래의 글과 함께 php로 개발중인데, 아무래도 데이터를 가져오고 출력하는 부분보다
그누보드 bbs/board.php, common.php 등 다양한 페이지에서 접근 권한 체크를 하고 있기 때문에 해당 부분과 동일한 접근 호출을 하는 api는 해당 페이지를 참조하여 권한 체크를 해주어야 합니다.
특히 is_member, is_guest, is_admin, member등은 거의 모든 페이지에서 권한 체크가 이루어지기 때문에 해당 부분은 아예 클래스내에서 전역변수처럼 활용가능하게 선언하여 사용하는것이 편합니다.
추후 어느정도 기개발 되면 깃헙에 해당 소스 전체는 공개하도록 하겠습니다.
댓글 4개
게시글 목록
| 번호 | 제목 |
|---|---|
| 1891 | |
| 1879 | |
| 1874 | |
| 1873 | |
| 1867 | |
| 1866 | |
| 1862 | |
| 1861 | |
| 1855 | |
| 1854 | |
| 1853 | |
| 1851 | |
| 1850 | |
| 1849 | |
| 1848 | |
| 1847 | |
| 1841 | |
| 1840 | |
| 1835 | |
| 1833 | |
| 1825 | |
| 1824 | |
| 1820 | |
| 1819 | |
| 1814 | |
| 1811 | |
| 1810 | |
| 1809 | |
| 1808 | |
| 1805 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기