/*******************************************************************************
 * Copyright 2014 2Fi Business Solutions Ltd.
 *
 * This code is part of the PCS project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.tbms.user.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ffii.core.utils.Params;
import com.ffii.core.utils.SecurityUtils;
import com.ffii.core.web.AbstractController;
import com.ffii.core.web.view.json.JsonView;
import com.ffii.tbms.user.service.AuthService;

@Controller
@RequestMapping(value = "/user")
public class UserAuthController extends AbstractController {

	@Autowired
	private AuthService authService;

	@RequestMapping(value = "/auth.json", method = { RequestMethod.GET, RequestMethod.POST })
	public String authJson(Model model, HttpServletRequest request,
			@RequestParam int userId) throws Exception {

		if (SecurityUtils.isGranted("USER_MAINTAIN")) {
			List<Map<String, Object>> records = authService.search(userId);
			model.addAttribute(Params.RECORDS, records);
		} else {
			model.addAttribute(Params.SUCCESS, Boolean.FALSE);
		}

		return JsonView.NAME;
	}

}
