Skip to content

Commit 4292caa

Browse files
committed
user 정보 관련 변수 수정 #1
1 parent 81ae4c2 commit 4292caa

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

app/interceptors/auth_interceptor.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,52 @@
22

33
require 'grpc'
44
require_relative '../../app/models/concerns/current'
5-
require_relative '../../app/models/concerns/simulated_user_roles' # 지금은 목데이터 사용
5+
require_relative '../../app/models/concerns/simulated_user_roles'
66

77
class AuthInterceptor < GRPC::ServerInterceptor
88
UNAUTHENTICATED = GRPC::BadStatus.new(
99
GRPC::Core::StatusCodes::UNAUTHENTICATED,
1010
"Unauthenticated: x-user-code metadata is missing"
1111
)
1212

13+
# UserRole enum 뒤 값 → 내부 authorize! 로 쓰는 문자열 매핑
14+
ROLE_MAP = {
15+
"STUDENT" => "student",
16+
"DOORKEEPER" => "doorkeeper",
17+
"CLASS_REP" => "class_rep",
18+
"TA" => "assistant",
19+
"PROFESSOR" => "professor",
20+
"ADMIN" => "admin",
21+
"DEFAULT" => "student"
22+
}.freeze
23+
1324
def request_response(request:, call:, method:)
1425
# HealthCheck는 인증 제외
1526
if method.to_s.match?(/Health|health/i)
1627
return yield
1728
end
1829

19-
# 필수: 유저 코드
2030
user_code = call.metadata['x-user-code']
21-
roles_header = (call.metadata['x-user-role'] || "student").to_s.downcase
31+
raw_roles = (call.metadata['x-user-role'] || "STUDENT").upcase
2232

2333
if user_code.nil? || user_code.empty?
2434
raise UNAUTHENTICATED
2535
end
2636

27-
# 권한이 여러 개 들어올 수 있으므로 배열로 변환
28-
roles = roles_header.split(',').map(&:strip)
29-
# 목데이터 사용으로 아래 코드 사용 유저 서비스 연결 시 주석처리 or 삭제
30-
highest_role = roles.max_by { |r| SimulatedUserRoles::AUTHORITY_LEVELS[r] || 0 }
37+
# 여러 권한이면 쉼표 기반 분리
38+
role_keys = raw_roles.split(",").map(&:strip)
39+
40+
# 서버에서 넘어온 enum 뒤 값 → 내부 role 로 매핑
41+
mapped_roles = role_keys.map { |key| ROLE_MAP[key] || "student" }
3142

32-
# 권한이 enum이 들어올 수도 있음 → 숫자로 변환 후 매핑 해야하나?
33-
# 유저 서비스 연결시 아래 코드 사용
34-
# roles_enum = roles_header.split(',').map(&:to_i)
35-
# roles = roles_enum.map { |v| USER_ROLE_MAP[v] }
36-
# highest_role = roles.max_by { |r| PRIORITY[r] }
43+
# SimulatedUserRoles 로 가장 높은 권한 찾기
44+
highest_role = mapped_roles.max_by { |r| SimulatedUserRoles::AUTHORITY_LEVELS[r] || 0 }
3745

38-
# 최종 적용
46+
# Current에 저장
3947
Current.user_code = user_code
4048
Current.user_role = highest_role
4149

42-
puts "[Auth] user_code=#{user_code}, roles=#{roles}, highest_role=#{highest_role}"
50+
puts "[Auth] user_code=#{user_code}, raw_roles=#{role_keys}, mapped_roles=#{mapped_roles}, highest_role=#{highest_role}"
4351

4452
yield
4553

0 commit comments

Comments
 (0)