// Path: src/main/java/com/restfiddle/service/auth/EntityAuthService.java
// @Service
// public class EntityAuthService {
// 
//     private static final String ENTITY_AUTH = "EntityAuth";
//     private static final String UNAUTHORIZED = "unauthorized";
//     private static final String SUCCESS = "success";
// 
//     @Autowired
//     private MongoTemplate mongoTemplate;
//     
//     public DBObject authenticate(JSONObject userDTO, String projectId) throws Exception{
// 	
// 	DBCollection dbCollection = mongoTemplate.getCollection(projectId+"_User");
// 	
// 	BasicDBObject query = new BasicDBObject();
// 	query.append("username", userDTO.get("username"));
// 	
// 	DBObject user = dbCollection.findOne(query);
// 	if(user == null){
// 	    throw new Exception("User not found");
// 	}
// 	
// 	BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
// 	BasicDBObject auth;
// 	if(encoder.matches((String)userDTO.get("password"),(String)user.get("password"))){
// 	    auth = new BasicDBObject();
// 	    auth.append("user", new DBRef(projectId+"_User",user.get( "_id" ))).append("expireAt", new Date(System.currentTimeMillis() + 3600 * 1000));
// 	    auth.put("projectId", projectId);
// 		
// 	    DBCollection dbCollectionAuth = mongoTemplate.getCollection(ENTITY_AUTH);
// 	    dbCollectionAuth.insert(auth);
// 	}else{
// 	   throw new Exception("Invalid password");
// 	}
// 	
// 	return auth;
//     }
//     
//     public boolean logout(String llt){
// 	
// 	DBCollection dbCollection = mongoTemplate.getCollection(ENTITY_AUTH);
// 	
// 	BasicDBObject queryObject = new BasicDBObject();
// 	queryObject.append("_id", new ObjectId(llt));
// 	WriteResult result = dbCollection.remove(queryObject);
// 	
// 	return result.getN() == 1;
//     }
// 
//     public JSONObject authorize(String projectId, String authToken, String... roles) {
// 	
// 	JSONObject response = new JSONObject();
// 	if(authToken == null){
// 	    return response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
// 	}
// 	
// 	List<String> roleList = Arrays.asList(roles);
// 	
// 	DBCollection dbCollection = mongoTemplate.getCollection(ENTITY_AUTH);
// 	
// 	BasicDBObject queryObject = new BasicDBObject();
// 	queryObject.append("_id", new ObjectId(authToken));
// 	
// 	DBObject authData = dbCollection.findOne(queryObject);
// 	
// 	if(authData != null && projectId.equals(authData.get("projectId"))) {
// 	    DBRef userRef = (DBRef)authData.get("user");
// 	    DBObject user = mongoTemplate.getCollection(userRef.getCollectionName()).findOne(userRef.getId());
// 	    
// 	    DBObject roleObj = null;
// 	    if(user.containsField("role")){
// 		DBRef roleRef = (DBRef)user.get("role");
// 		roleObj = mongoTemplate.getCollection(roleRef.getCollectionName()).findOne(roleRef.getId());
// 	    }
// 	    
// 	    if((roleObj != null && roleList.contains(roleObj.get("name"))) || roleList.contains("USER")){
// 		response.put(SUCCESS, true);
// 		response.put("user", userRef);
// 		
// 		authData.put("expireAt", new Date(System.currentTimeMillis() + 3600 * 1000));
// 		dbCollection.save(authData);
// 	    } else {
// 		response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
// 	    }
// 	} else {
// 	    response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
// 	}
// 	
// 	return response;
//     }
// }


import java.util.Map;
import org.bson.types.ObjectId;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.restfiddle.service.auth.EntityAuthService;
/*
 * Copyright 2015 Ranjan Kumar
 *
 * 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 com.restfiddle.controller.rest;




@RestController
@Transactional
public class EntitySessionController {
    Logger logger = LoggerFactory.getLogger(EntitySessionController.class);

    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Autowired
// Path: src/main/java/com/restfiddle/service/auth/EntityAuthService.java
// @Service
// public class EntityAuthService {
// 
//     private static final String ENTITY_AUTH = "EntityAuth";
//     private static final String UNAUTHORIZED = "unauthorized";
//     private static final String SUCCESS = "success";
// 
//     @Autowired
//     private MongoTemplate mongoTemplate;
//     
//     public DBObject authenticate(JSONObject userDTO, String projectId) throws Exception{
// 	
// 	DBCollection dbCollection = mongoTemplate.getCollection(projectId+"_User");
// 	
// 	BasicDBObject query = new BasicDBObject();
// 	query.append("username", userDTO.get("username"));
// 	
// 	DBObject user = dbCollection.findOne(query);
// 	if(user == null){
// 	    throw new Exception("User not found");
// 	}
// 	
// 	BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
// 	BasicDBObject auth;
// 	if(encoder.matches((String)userDTO.get("password"),(String)user.get("password"))){
// 	    auth = new BasicDBObject();
// 	    auth.append("user", new DBRef(projectId+"_User",user.get( "_id" ))).append("expireAt", new Date(System.currentTimeMillis() + 3600 * 1000));
// 	    auth.put("projectId", projectId);
// 		
// 	    DBCollection dbCollectionAuth = mongoTemplate.getCollection(ENTITY_AUTH);
// 	    dbCollectionAuth.insert(auth);
// 	}else{
// 	   throw new Exception("Invalid password");
// 	}
// 	
// 	return auth;
//     }
//     
//     public boolean logout(String llt){
// 	
// 	DBCollection dbCollection = mongoTemplate.getCollection(ENTITY_AUTH);
// 	
// 	BasicDBObject queryObject = new BasicDBObject();
// 	queryObject.append("_id", new ObjectId(llt));
// 	WriteResult result = dbCollection.remove(queryObject);
// 	
// 	return result.getN() == 1;
//     }
// 
//     public JSONObject authorize(String projectId, String authToken, String... roles) {
// 	
// 	JSONObject response = new JSONObject();
// 	if(authToken == null){
// 	    return response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
// 	}
// 	
// 	List<String> roleList = Arrays.asList(roles);
// 	
// 	DBCollection dbCollection = mongoTemplate.getCollection(ENTITY_AUTH);
// 	
// 	BasicDBObject queryObject = new BasicDBObject();
// 	queryObject.append("_id", new ObjectId(authToken));
// 	
// 	DBObject authData = dbCollection.findOne(queryObject);
// 	
// 	if(authData != null && projectId.equals(authData.get("projectId"))) {
// 	    DBRef userRef = (DBRef)authData.get("user");
// 	    DBObject user = mongoTemplate.getCollection(userRef.getCollectionName()).findOne(userRef.getId());
// 	    
// 	    DBObject roleObj = null;
// 	    if(user.containsField("role")){
// 		DBRef roleRef = (DBRef)user.get("role");
// 		roleObj = mongoTemplate.getCollection(roleRef.getCollectionName()).findOne(roleRef.getId());
// 	    }
// 	    
// 	    if((roleObj != null && roleList.contains(roleObj.get("name"))) || roleList.contains("USER")){
// 		response.put(SUCCESS, true);
// 		response.put("user", userRef);
// 		
// 		authData.put("expireAt", new Date(System.currentTimeMillis() + 3600 * 1000));
// 		dbCollection.save(authData);
// 	    } else {
// 		response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
// 	    }
// 	} else {
// 	    response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
// 	}
// 	
// 	return response;
//     }
// }


// Path: src/main/java/com/restfiddle/controller/rest/EntitySessionController.java
import java.util.Map;
import org.bson.types.ObjectId;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.restfiddle.service.auth.EntityAuthService;

/*
 * Copyright 2015 Ranjan Kumar
 *
 * 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 com.restfiddle.controller.rest;




@RestController
@Transactional
public class EntitySessionController {
    Logger logger = LoggerFactory.getLogger(EntitySessionController.class);

    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Autowired
    private EntityAuthService authService;