/*
 * Copyright 2015 ZJNU ACM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zjnu.acm.judge.service;

import cn.edu.zjnu.acm.judge.domain.User;
import cn.edu.zjnu.acm.judge.domain.UserModel;
import cn.edu.zjnu.acm.judge.exception.ForbiddenException;
import cn.edu.zjnu.acm.judge.mapper.UserMapper;
import cn.edu.zjnu.acm.judge.mapper.UserRoleMapper;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 *
 * @author zhanhb
 */
@Service
@Slf4j
public class UserDetailService {

    private static final List<GrantedAuthority> ROLE_ADMIN = Collections.unmodifiableList(AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_SOURCE_BROWSER", "ROLE_USER"));
    private static final List<GrantedAuthority> ROLE_SOURCE_BROWSER = Collections.unmodifiableList(AuthorityUtils.createAuthorityList("ROLE_SOURCE_BROWSER", "ROLE_USER"));
    private static final List<GrantedAuthority> ROLE_USER = Collections.unmodifiableList(AuthorityUtils.createAuthorityList("ROLE_USER"));
    private static final List<List<GrantedAuthority>> ROLES = Arrays.asList(ROLE_USER, ROLE_SOURCE_BROWSER, ROLE_ADMIN);

    public static void requireAdminLoginned(HttpServletRequest request) {
        if (!isAdminLoginned(request)) {
            throw new ForbiddenException();
        }
    }

    public static boolean isAdminLoginned(HttpServletRequest request) {
        return request.isUserInRole("ADMIN");
    }

    public static boolean isSourceBrowser(HttpServletRequest request) {
        return request.isUserInRole("SOURCE_BROWSER");
    }

    public static boolean isLoginned(HttpServletRequest request) {
        return request.isUserInRole("USER");
    }

    @Nonnull
    public static Optional<UserModel> getCurrentUser(@Nonnull HttpServletRequest request) {
        return Optional.ofNullable(request.getUserPrincipal())
                .filter(Authentication.class::isInstance)
                .map(Authentication.class::cast)
                .map(Authentication::getPrincipal)
                .filter(UserModel.class::isInstance)
                .map(UserModel.class::cast);
    }

    @Nonnull
    public static Optional<String> getCurrentUserId(@Nonnull HttpServletRequest request) {
        return getCurrentUser(request).map(UserModel::getUserId);
    }

    public static boolean isUser(HttpServletRequest request, String userId) {
        return userId != null && getCurrentUserId(request).map(userId::equalsIgnoreCase).orElse(false);
    }

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;

    public UserModel getUserModel(String userId) {
        User user = userMapper.findOne(userId);
        if (user == null) {
            throw new UsernameNotFoundException(userId);
        }
        int role = 0;
        for (String rightstr : userRoleMapper.findAllByUserId(user.getId())) {
            if (rightstr != null) {
                switch (rightstr.toLowerCase()) {
                    case "administrator":
                        role = 2;
                        break;
                    case "source_browser":
                        role = Math.max(role, 1);
                        break;
                    case "news_publisher":
                        // TODO
                        break;
                    default:
                        break;
                }
            }
        }

        return new UserModel(user, ROLES.get(role));
    }

}

@@ -2595,127 +2595,8 @@
  %7D%0A%0A
-    public static boolean isLoginned(HttpServletRequest request) %7B%0A        return request.isUserInRole(%22USER%22);%0A    %7D%0A%0A
     

Remove method isLoginned in user detail service