Files
sam-api/app/Models/Boards/Board.php
hskwon d27e47108d feat: [boards] 게시판 API 시스템 구현
- BoardController, PostController 추가
- Board, BoardSetting 모델 수정
- BoardService 추가
- FormRequest 클래스 추가
- Swagger 문서 추가 (BoardApi, PostApi)
- 게시판 시스템 필드 마이그레이션 추가
2025-11-30 21:05:33 +09:00

216 lines
5.4 KiB
PHP

<?php
namespace App\Models\Boards;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 게시판 모델
*
* @property int $id
* @property int|null $tenant_id
* @property bool $is_system 시스템 게시판 여부 (true=전체 테넌트 공개)
* @property string|null $board_type 게시판 유형 (notice, qna, faq, free 등)
* @property string $board_code 게시판 코드
* @property string $name 게시판명
* @property string|null $description 설명
* @property string $editor_type 에디터 타입
* @property bool $allow_files 파일 첨부 허용
* @property int $max_file_count 최대 파일 수
* @property int $max_file_size 최대 파일 크기 (KB)
* @property array|null $extra_settings 추가 설정 (권한, 옵션 등)
* @property bool $is_active 활성 여부
*
* @mixin IdeHelperBoard
*/
class Board extends Model
{
use SoftDeletes;
protected $table = 'boards';
protected $fillable = [
'tenant_id',
'is_system',
'board_type',
'board_code',
'name',
'description',
'editor_type',
'allow_files',
'max_file_count',
'max_file_size',
'extra_settings',
'is_active',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'is_system' => 'boolean',
'is_active' => 'boolean',
'allow_files' => 'boolean',
'extra_settings' => 'array',
];
protected $attributes = [
'is_system' => false,
'is_active' => true,
'allow_files' => true,
'max_file_count' => 5,
'max_file_size' => 20480,
'editor_type' => 'wysiwyg',
];
// =========================================================================
// Scopes
// =========================================================================
/**
* 현재 테넌트에서 접근 가능한 게시판
* - 시스템 게시판 (is_system = true)
* - 해당 테넌트 게시판 (tenant_id = 현재 테넌트)
*/
public function scopeAccessible(Builder $query, int $tenantId): Builder
{
return $query->where(function ($q) use ($tenantId) {
$q->where('is_system', true)
->orWhere('tenant_id', $tenantId);
})->where('is_active', true);
}
/**
* 시스템 게시판만 (mng용)
*/
public function scopeSystemOnly(Builder $query): Builder
{
return $query->where('is_system', true);
}
/**
* 테넌트 게시판만
*/
public function scopeTenantOnly(Builder $query, int $tenantId): Builder
{
return $query->where('is_system', false)
->where('tenant_id', $tenantId);
}
/**
* 게시판 유형으로 필터링
*/
public function scopeOfType(Builder $query, string $type): Builder
{
return $query->where('board_type', $type);
}
// =========================================================================
// Relationships
// =========================================================================
public function customFields()
{
return $this->hasMany(BoardSetting::class, 'board_id')
->orderBy('sort_order');
}
public function fields()
{
return $this->customFields();
}
public function posts()
{
return $this->hasMany(Post::class, 'board_id');
}
public function tenant()
{
return $this->belongsTo(\App\Models\Tenant::class);
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by');
}
public function updater()
{
return $this->belongsTo(User::class, 'updated_by');
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* extra_settings에서 특정 키 값 가져오기
*/
public function getSetting(string $key, $default = null)
{
return data_get($this->extra_settings, $key, $default);
}
/**
* extra_settings에 값 설정하기
*/
public function setSetting(string $key, $value): self
{
$settings = $this->extra_settings ?? [];
data_set($settings, $key, $value);
$this->extra_settings = $settings;
return $this;
}
/**
* 읽기 권한 확인
*/
public function canRead(User $user): bool
{
$roles = $this->getSetting('permissions.read', ['*']);
return in_array('*', $roles) || $user->hasAnyRole($roles);
}
/**
* 쓰기 권한 확인
*/
public function canWrite(User $user): bool
{
$roles = $this->getSetting('permissions.write', ['*']);
return in_array('*', $roles) || $user->hasAnyRole($roles);
}
/**
* 관리 권한 확인
*/
public function canManage(User $user): bool
{
$roles = $this->getSetting('permissions.manage', ['admin']);
return $user->hasAnyRole($roles);
}
/**
* 시스템 게시판 여부 확인
*/
public function isSystemBoard(): bool
{
return $this->is_system === true;
}
/**
* 테넌트 게시판 여부 확인
*/
public function isTenantBoard(): bool
{
return $this->is_system === false;
}
}