@@ -38,6 +38,7 @@ var _ IAuthService = (*AuthService)(nil)
3838type IAuthService interface {
3939 SignIn (ctx context.Context , req * dto.SignInReq ) (resp * dto.SignInResp , err error )
4040 IsAdmin (ctx context.Context ) (resp * dto.IsAdminResp , err error )
41+ GrantAdmin (ctx context.Context , req * dto.GrantAdminReq ) (resp * dto.GrantAdminResp , err error )
4142}
4243
4344type AuthService struct {
@@ -150,3 +151,33 @@ func (s *AuthService) IsAdmin(ctx context.Context) (resp *dto.IsAdminResp, err e
150151 Resp : dto .Success (),
151152 }, nil
152153}
154+
155+ func (s * AuthService ) GrantAdmin (ctx context.Context , req * dto.GrantAdminReq ) (resp * dto.GrantAdminResp , err error ) {
156+ // 鉴权
157+ userId , ok := ctx .Value (consts .CtxUserID ).(string )
158+ if ! ok || userId == "" {
159+ return nil , errorx .New (errno .ErrUserNotLogin )
160+ }
161+
162+ // 判断用户是否为管理员
163+ admin , err := s .UserRepo .IsAdminByID (ctx , req .UserID )
164+ if err != nil {
165+ return nil , errorx .WrapByCode (err , errno .ErrUserFindFailed ,
166+ errorx .KV ("key" , consts .CtxUserID ), errorx .KV ("value" , req .UserID ),
167+ )
168+ }
169+ if admin {
170+ return nil , errorx .New (errno .ErrUserAlreadyAdmin , errorx .KV ("id" , req .UserID ))
171+ }
172+
173+ // 添加管理员
174+ if err = s .UserRepo .Update (ctx , & model.User {ID : req .UserID , Admin : true }); err != nil {
175+ return nil , errorx .WrapByCode (err , errno .ErrUserUpdateFailed ,
176+ errorx .KV ("key" , consts .CtxUserID ), errorx .KV ("value" , req .UserID ),
177+ )
178+ }
179+
180+ return & dto.GrantAdminResp {
181+ Resp : dto .Success (),
182+ }, nil
183+ }
0 commit comments