public HttpResponseMessage<bool> Login(HttpRequestMessage request) { var connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString; var isOkay = Authenticator.Authenticate(connectionstring); if (isOkay) { //Guardo un nuevo Scoring ScoreRepository scoreRepository = new ScoreRepository(connectionstring); scoreRepository.AddLoginScore(System.Web.HttpContext.Current.User.Identity.Name); if (!Session.HasLoggedUser()) { EarthwatcherRepository ewRepo = new EarthwatcherRepository(connectionstring); var ew = ewRepo.GetEarthwatcher(System.Web.HttpContext.Current.User.Identity.Name, false); if (ew != null) { Session.GenerateCookie(ew); } } return new HttpResponseMessage<bool>(isOkay) { StatusCode = HttpStatusCode.OK }; } else { return new HttpResponseMessage<bool>(isOkay) { StatusCode = HttpStatusCode.Forbidden }; } }
/// <summary> /// Si ya la habia reportado la pasa a greenpeace, sino la deja para asignar /// </summary> /// <param name="earthwatcherId"></param> /// <param name="basecamp"></param> /// <returns></returns> public LandMini ReassignLand(int earthwatcherId) { try { var earthwatcherRepository = new EarthwatcherRepository(connection.ConnectionString); var earthwatcher = earthwatcherRepository.GetEarthwatcher(earthwatcherId); var newLand = earthwatcherRepository.GetFreeLandByEarthwatcherIdAndPlayingRegion(earthwatcherId, earthwatcher.PlayingRegion); if (newLand != null && !String.IsNullOrEmpty(newLand.GeohexKey)) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Land_ReassignLandByEarthwatcherId"; command.Parameters.Add(new SqlParameter("@RegionId", earthwatcher.PlayingRegion)); command.Parameters.Add(new SqlParameter("@EarthwatcherId", earthwatcherId)); command.Parameters.Add(new SqlParameter("@newGeoHexKey", newLand.GeohexKey)); command.ExecuteNonQuery(); connection.Close(); return(newLand); } else { return(null); } } catch (Exception ex) { throw ex; } }
public HttpResponseMessage<List<Rank>> GetContestLeaderBoard(int userid, HttpRequestMessage request) { try { IEarthwatcherRepository ewRepo = new EarthwatcherRepository(connectionstring); var earthwatcher = ewRepo.GetEarthwatcher(userid); var leaderBoard = scoreRepository.GetLeaderBoardNationalRanking(true, earthwatcher.PlayingRegion); var rankingCollection = leaderBoard.Take(10).ToList(); if (!rankingCollection.Any(x => x.EarthwatcherId == userid)) { rankingCollection.Add(leaderBoard.Where(x => x.EarthwatcherId == userid).First()); } foreach (var r in rankingCollection) { if (string.IsNullOrEmpty(r.Name)) { r.Name = r.Nick; } } return new HttpResponseMessage<List<Rank>>(rankingCollection) { StatusCode = HttpStatusCode.OK }; } catch (Exception ex) { return new HttpResponseMessage<List<Rank>>(null) { StatusCode = HttpStatusCode.InternalServerError, ReasonPhrase = ex.Message }; } }
public void GenerateAndUpdatePassword(Earthwatcher earthwatcher) { var prefix = PasswordChecker.GeneratePrefix(); var hashedPassword = PasswordChecker.GenerateHashedPassword(earthwatcher.Password, prefix); // store in database var connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString; var repos = new EarthwatcherRepository(connectionstring); repos.UpdatePassword(earthwatcher, prefix, hashedPassword); }
private static bool TryGetPrincipal(string connectionString, string userName, string password, out IPrincipal principal) { var earthwatcher = new EarthwatcherRepository(connectionString).GetEarthwatcher(userName, false); if (earthwatcher != null) { var membershipProvider = new EarthwatchersMembershipProvider(connectionString); var result = membershipProvider.ValidateUser(userName, password, earthwatcher.ApiEwId); if (result) { principal = new GenericPrincipal(new GenericIdentity(userName), earthwatcher.GetRoles()); return true; } } principal = null; return false; }
public bool ValidateUser(string username, string password, int apiEwId = 0) { var isValid = false; ApiEw apiEw = null; EarthwatcherRepository earthwatcherRepo = new EarthwatcherRepository(connectionString); if(apiEwId != 0) { apiEw = earthwatcherRepo.GetApiEwById(apiEwId); } if (apiEw == null) { var credentials = CredentialsRepository.GetCredentials(connectionString, username); isValid = PasswordChecker.CheckPassword(password, credentials.Prefix, credentials.Hash); } else { //TODO: Validacion de AccessToken, por ahora si entro con una api lo manda derecho isValid = true; } return isValid; }
public static Earthwatcher CurrentUser() { if (!HttpContext.Current.Request.Headers.AllKeys.Contains("Authorization")) return null; string authHeader = HttpContext.Current.Request.Headers["Authorization"]; var creds = ParseAuthHeader(authHeader); if(creds.Length < 2) return null; EarthwatcherRepository repo = new EarthwatcherRepository(System.Configuration.ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString); var user = repo.GetEarthwatcher(creds[0], false); return user; }
/// <summary> /// Si ya la habia reportado la pasa a greenpeace, sino la deja para asignar /// </summary> /// <param name="earthwatcherId"></param> /// <param name="basecamp"></param> /// <returns></returns> public LandMini ReassignLand(int earthwatcherId) { try { var earthwatcherRepository = new EarthwatcherRepository(connection.ConnectionString); var earthwatcher = earthwatcherRepository.GetEarthwatcher(earthwatcherId); var newLand = earthwatcherRepository.GetFreeLandByEarthwatcherIdAndPlayingRegion(earthwatcherId, earthwatcher.PlayingRegion); if (newLand != null && !String.IsNullOrEmpty(newLand.GeohexKey)) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Land_ReassignLandByEarthwatcherId"; command.Parameters.Add(new SqlParameter("@RegionId", earthwatcher.PlayingRegion)); command.Parameters.Add(new SqlParameter("@EarthwatcherId", earthwatcherId)); command.Parameters.Add(new SqlParameter("@newGeoHexKey", newLand.GeohexKey)); command.ExecuteNonQuery(); connection.Close(); return newLand; } else return null; } catch(Exception ex) { throw ex; } }
//JAMAS SE USO, SE LLAMA DEL Backend public bool MassiveReassign(int regionId) { Dictionary<int, string> updateLands = new Dictionary<int, string>(); string newGeoHexKeys = string.Empty; bool success = true; try { //1. Traigo todos los lands asignados connection.Open(); var command = connection.CreateCommand() as SqlCommand; command.CommandType = CommandType.StoredProcedure; var lands = connection.Query<Land>("EXEC Land_MassiveReassign_sql").ToList(); connection.Close(); EarthwatcherRepository earthwatcherRepository = new EarthwatcherRepository(this.connection.ConnectionString); Random rand = new Random(); //2. Genero un Random para tener 1/3 de posibilidades de asignar una tierra nueva y 2/3 de una tierra usada int r = rand.Next(1, 4); for (int i = 0; i < lands.Count; i++) { var land = lands[i]; if (updateLands.ContainsKey(land.EarthwatcherId.Value)) { continue; } //3. El 1 y el 2 corresponden a asignar una parcela usada y el if (i > 0) { r++; if (r > 3) { r = 1; } } if (lands.Count > (updateLands.Count + 1)) { if (r < 3) { //Parcela Usada int ru = rand.Next(0, lands.Count - 1 - updateLands.Count); //var l = lands.Where(x => x.Id != land.Id && !updateLands.ContainsKey(x.EarthwatcherId.Value) && !updateLands.ContainsValue(x.GeohexKey)).Skip(ru).FirstOrDefault(); var oldland = (from l in lands where (from u in updateLands where u.Key == l.EarthwatcherId.Value select u).Count() == 0 select l).Skip(ru).FirstOrDefault(); if (oldland != null) { updateLands.Add(land.EarthwatcherId.Value, oldland.GeohexKey); if (!updateLands.ContainsKey(oldland.EarthwatcherId.Value)) { updateLands.Add(oldland.EarthwatcherId.Value, land.GeohexKey); } } } else { var newFreeLand = earthwatcherRepository.GetFreeLand(regionId, newGeoHexKeys); if (newFreeLand != null) { //Parcela Nueva if (newGeoHexKeys != string.Empty) { newGeoHexKeys += ","; } newGeoHexKeys += "'" + land.GeohexKey + "'"; newGeoHexKeys += ",'" + newFreeLand.GeohexKey + "'"; updateLands.Add(land.EarthwatcherId.Value, newFreeLand.GeohexKey); } } } else { //Ultima parcela impar //Parcela Nueva var newland = (from l in lands where (from u in updateLands where u.Key == l.EarthwatcherId.Value select u).Count() == 0 select l).FirstOrDefault(); if (newland != null) { var newFreeLand = earthwatcherRepository.GetFreeLand(regionId, newGeoHexKeys); if (newFreeLand != null) { if (newGeoHexKeys != string.Empty) { newGeoHexKeys += ","; } newGeoHexKeys += "'" + land.GeohexKey + "'"; newGeoHexKeys += ",'" + newFreeLand.GeohexKey + "'"; updateLands.Add(newland.EarthwatcherId.Value, newFreeLand.GeohexKey); } } break; } } DataTable dt = new DataTable(); dt.Columns.Add("EarthwatcherId", typeof(int)); dt.Columns.Add("GeohexKey", typeof(string)); foreach (var land in updateLands) { DataRow row = dt.NewRow(); row[0] = land.Key; row[1] = land.Value; dt.Rows.Add(row); } connection.Open(); command.CommandText = "Lands_Reassign"; command.Parameters.Add(new SqlParameter { ParameterName = "@lands", SqlDbType = System.Data.SqlDbType.Structured, Value = dt }); command.ExecuteNonQuery(); } catch (Exception ex) { success = false; throw ex; } finally { connection.Close(); } return success; }
public bool MassiveReassign(int regionId) //JAMAS SE USO, SE LLAMA DEL Backend { Dictionary <int, string> updateLands = new Dictionary <int, string>(); string newGeoHexKeys = string.Empty; bool success = true; try { //1. Traigo todos los lands asignados connection.Open(); var command = connection.CreateCommand() as SqlCommand; command.CommandType = CommandType.StoredProcedure; var lands = connection.Query <Land>("EXEC Land_MassiveReassign_sql").ToList(); connection.Close(); EarthwatcherRepository earthwatcherRepository = new EarthwatcherRepository(this.connection.ConnectionString); Random rand = new Random(); //2. Genero un Random para tener 1/3 de posibilidades de asignar una tierra nueva y 2/3 de una tierra usada int r = rand.Next(1, 4); for (int i = 0; i < lands.Count; i++) { var land = lands[i]; if (updateLands.ContainsKey(land.EarthwatcherId.Value)) { continue; } //3. El 1 y el 2 corresponden a asignar una parcela usada y el if (i > 0) { r++; if (r > 3) { r = 1; } } if (lands.Count > (updateLands.Count + 1)) { if (r < 3) { //Parcela Usada int ru = rand.Next(0, lands.Count - 1 - updateLands.Count); //var l = lands.Where(x => x.Id != land.Id && !updateLands.ContainsKey(x.EarthwatcherId.Value) && !updateLands.ContainsValue(x.GeohexKey)).Skip(ru).FirstOrDefault(); var oldland = (from l in lands where (from u in updateLands where u.Key == l.EarthwatcherId.Value select u).Count() == 0 select l).Skip(ru).FirstOrDefault(); if (oldland != null) { updateLands.Add(land.EarthwatcherId.Value, oldland.GeohexKey); if (!updateLands.ContainsKey(oldland.EarthwatcherId.Value)) { updateLands.Add(oldland.EarthwatcherId.Value, land.GeohexKey); } } } else { var newFreeLand = earthwatcherRepository.GetFreeLand(regionId, newGeoHexKeys); if (newFreeLand != null) { //Parcela Nueva if (newGeoHexKeys != string.Empty) { newGeoHexKeys += ","; } newGeoHexKeys += "'" + land.GeohexKey + "'"; newGeoHexKeys += ",'" + newFreeLand.GeohexKey + "'"; updateLands.Add(land.EarthwatcherId.Value, newFreeLand.GeohexKey); } } } else { //Ultima parcela impar //Parcela Nueva var newland = (from l in lands where (from u in updateLands where u.Key == l.EarthwatcherId.Value select u).Count() == 0 select l).FirstOrDefault(); if (newland != null) { var newFreeLand = earthwatcherRepository.GetFreeLand(regionId, newGeoHexKeys); if (newFreeLand != null) { if (newGeoHexKeys != string.Empty) { newGeoHexKeys += ","; } newGeoHexKeys += "'" + land.GeohexKey + "'"; newGeoHexKeys += ",'" + newFreeLand.GeohexKey + "'"; updateLands.Add(newland.EarthwatcherId.Value, newFreeLand.GeohexKey); } } break; } } DataTable dt = new DataTable(); dt.Columns.Add("EarthwatcherId", typeof(int)); dt.Columns.Add("GeohexKey", typeof(string)); foreach (var land in updateLands) { DataRow row = dt.NewRow(); row[0] = land.Key; row[1] = land.Value; dt.Rows.Add(row); } connection.Open(); command.CommandText = "Lands_Reassign"; command.Parameters.Add(new SqlParameter { ParameterName = "@lands", SqlDbType = System.Data.SqlDbType.Structured, Value = dt }); command.ExecuteNonQuery(); } catch (Exception ex) { success = false; throw ex; } finally { connection.Close(); } return(success); }
public LandMini ReassignLand(int earthwatcherId, string basecamp) //Si ya la habia reportado la pasa a greenpeace, sino la deja para asignar { var earthwatcherRepository = new EarthwatcherRepository(connection.ConnectionString); var newLand = earthwatcherRepository.GetFreeLandByEarthwatcherId(basecamp, earthwatcherId); if (newLand != null && !String.IsNullOrEmpty(newLand.GeohexKey)) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Land_ReassignLandByEarthwatcherId"; command.Parameters.Add(new SqlParameter("@EarthwatcherId", earthwatcherId)); command.Parameters.Add(new SqlParameter("@newGeoHexKey", newLand.GeohexKey)); command.ExecuteNonQuery(); connection.Close(); return newLand; } else return null; }
//NO SE USA public LandMini ReassignLand(Land land, string basecamp) { var earthwatcherRepository = new EarthwatcherRepository(connection.ConnectionString); var newLand = earthwatcherRepository.GetFreeLand(basecamp, "'" + land.GeohexKey + "'"); if (newLand != null && !String.IsNullOrEmpty(newLand.GeohexKey)) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Land_ReassignLand"; command.Parameters.Add(new SqlParameter("@EarthwatcherId", land.EarthwatcherId)); command.Parameters.Add(new SqlParameter("@currentLand", land.Id)); command.Parameters.Add(new SqlParameter("@newGeoHexKey", newLand.GeohexKey)); command.Parameters.Add(new SqlParameter("@status", Convert.ToInt32(land.LandStatus))); command.ExecuteNonQuery(); connection.Close(); return newLand; } else return null; }