Controllers thay đổi

DepartmentController

Thay đổi chính

  1. company_id lấy từ auth — không nhận từ request (fix lỗi bảo mật)
  2. Company scope check — kiểm tra department có thuộc company của employee không
  3. Spatie permission middleware — route đã có middleware permission:department.xxx
public function createDepartment(Request $request) {
    $user = employee_account();
    $companyId = $user->getCompanyAttribute()?->id;

    $data = $request->validate([
        'title' => 'string|required|max:255',
        'status' => 'string|required|in:active,inactive',
        'parent_id' => 'nullable|integer|exists:departments,id',
        // KHÔNG còn company_id từ request
    ]);

    $data['company_id'] = $companyId; // Lấy từ auth

    if(isset($data['parent_id'])) {
        $parentDept = $this->departmentService->findById($data['parent_id']);
        // Kiểm tra parent thuộc cùng company
        if ($parentDept->company_id != $companyId) {
            return $this->error('Parent department does not belong to your company.', 403);
        }
    }
}

CompanyBranchController

Mới hoàn toàn (trước đây là class rỗng)

class CompanyBranchController extends APIController
{
    protected ICompanyBranchService $branchService;

    public function __construct(ICompanyBranchService $branchService) {
        $this->branchService = $branchService;
    }

    public function getAll()     { return $this->success($this->branchService->getAll()); }
    public function getById($id) { /* tìm và trả về */ }
    public function create(Request $request) { /* validate + tạo */ }
    public function update($id, Request $request) { /* validate + cập nhật */ }
    public function delete($id) { /* xoá */ }
}

CompanyController

Thay đổi

EmployeeAccountController

Thay đổi

← Hướng dẫn sử dụng trong code API Flow →