Ejemplo n.º 1
0
 public login()
 {
     _localizationService = new LocalizationService();
 }
        public HttpResponseMessage<Earthwatcher> LoginWithApi(ApiEw ew, HttpRequestMessage<ApiEw> request)
        {
            var localization = new LocalizationService();
            string locale = localization.GetLocale();

            if(!string.IsNullOrEmpty(ew.UserId))
            {
                ApiEw apiEw = earthwatcherRepository.GetApiEw(ew.Api, ew.UserId);

                if (apiEw == null)  //Si no existe en la tabla ApiEwLogin lo inserta y ademas Crea el EARTHWATCHER
                {
                    //INSERTA EL ApiEw en la tabla ApiEwLogin
                    ApiEw newApiEw = earthwatcherRepository.CreateApiEwLogin(ew);

                    if(ew.Api == "Facebook")
                    {
                        Earthwatcher ewIdemFbMail = earthwatcherRepository.GetEarthwatcher(ew.Mail, false);

                        if(ewIdemFbMail != null)  //Si en FB tiene el mismo mail que en guardianes lo relaciona
                        {
                            //Relaciona al ApiEw de facebook con el Earthwatcher del mismo mail
                            earthwatcherRepository.LinkApiAndEarthwatcher(newApiEw.Id, ewIdemFbMail.Id);

                            //Le pasa el UserId y AccessToken para conectarse
                            ewIdemFbMail.UserId = ew.UserId;
                            ewIdemFbMail.AccessToken = ew.AccessToken;

                            //Devuelve el Ew que YA EXISTE relacionado con ese mail de FB
                            return new HttpResponseMessage<Earthwatcher>(ewIdemFbMail) { StatusCode = HttpStatusCode.OK };
                        }
                        else
                        {
                            //INSERTA EL EARTHWATCHER
                            var earthwatcher = new Earthwatcher();
                            if(!string.IsNullOrEmpty(ew.Mail))
                            {
                                earthwatcher.Name = ew.Mail;   //Ingreso el mail valido de ese Ew
                            }
                            else
                            {
                                earthwatcher.Name = ew.NickName + ew.UserId.ToString();  //Ingreso un customMail para ese EW
                            }
                            earthwatcher.Password = ew.NickName + earthwatcher.NickName + ew.UserId.ToString(); //Pass = Doble nombre mas userId
                            earthwatcher.NickName = ew.NickName;
                            earthwatcher.Language = (locale != null) ? locale.Substring(0, 5) : "en-CA";
                            earthwatcher.Country = (locale != null) ? locale.Substring(3, 2) : "es-AR";
                            earthwatcher.PlayingRegion = (locale != null) ? Convert.ToInt32(locale.Substring(6, 1)) : 1;
                            earthwatcher.PlayingCountry = (regionRepository.GetById(earthwatcher.PlayingRegion)).CountryCode;
                            earthwatcher.Guid = Guid.NewGuid();

                            Earthwatcher newEarthwatcher = earthwatcherRepository.CreateEarthwatcher(earthwatcher);

                            //Relaciona al ApiEw con el Earthwatcher
                            earthwatcherRepository.LinkApiAndEarthwatcher(newApiEw.Id, newEarthwatcher.Id);

                            //ASIGNO UNA LAND AL NUEVO EW
                            var newLand = earthwatcherRepository.AssignLandToEarthwatcher(earthwatcher.Id, string.Empty);
                            Land newLandObj = null;
                            Console.WriteLine("Me paso la new land " + newLand.Id + " " + newLand.LandId);
                            if (newLand == null) //Si la region esta completa le asigno la land del tutor
                            {
                                newLandObj = landRepository.GetTutorLand(earthwatcher.PlayingRegion);
                            }
                            if (newLandObj == null) //Si no esta completa la region
                            {
                                newLandObj = landRepository.GetLandByGeoHexKey(newLand.GeohexKey);
                            }
                            earthwatcher.Lands = new List<Land>();
                            earthwatcher.Lands.Add(newLandObj);
                            //Comunico a los usuarios conectados si es que la nueva land es de un usuario existente
                            NotificateUsers(newLand, earthwatcher.Id);

                            //Le pasa el D:\Dev\Greenpeace\Guardianes\Earthwatchers.UI\Requests\OpengeocoderRequests.csUserId y AccessToken para conectarse
                            newEarthwatcher.UserId = ew.UserId;
                            newEarthwatcher.AccessToken = ew.AccessToken;

                            //Devuelve el ew NUEVO relacionado con esa nueva cuenta de FB
                            return new HttpResponseMessage<Earthwatcher>(newEarthwatcher) { StatusCode = HttpStatusCode.OK };
                        }

                    }
                    if (ew.Api != "Facebook")
                    {
                        //INSERTA EL EARTHWATCHER
                        var earthwatcher = new Earthwatcher();
                        earthwatcher.Name = ew.NickName + ew.UserId.ToString();
                        earthwatcher.Password = ew.NickName + earthwatcher.NickName + ew.UserId.ToString(); //Pass = Doble nombre mas userId
                        earthwatcher.NickName = ew.NickName;
                        earthwatcher.Language = (locale != null) ? locale.Substring(0, 5) : "en-CA";
                        earthwatcher.Country = (locale != null) ? locale.Substring(3, 2) : "es-AR";
                        earthwatcher.PlayingRegion = (locale != null) ? Convert.ToInt32(locale.Substring(6, 1)) : 1;
                        earthwatcher.PlayingCountry = (regionRepository.GetById(earthwatcher.PlayingRegion)).CountryCode;
                        earthwatcher.Guid = Guid.NewGuid();

                        Earthwatcher newEarthwatcher = earthwatcherRepository.CreateEarthwatcher(earthwatcher);

                        //Relaciona al ApiEw con el Earthwatcher
                        earthwatcherRepository.LinkApiAndEarthwatcher(newApiEw.Id, newEarthwatcher.Id);

                        var newLand = earthwatcherRepository.AssignLandToEarthwatcher(earthwatcher.Id, string.Empty);
                        Land newLandObj = null;
                        Console.WriteLine("Me paso la new land " + newLand.Id + " " + newLand.LandId);
                        if (newLand == null) //Si la region esta completa le asigno la land del tutor
                        {
                            newLandObj = landRepository.GetTutorLand(earthwatcher.PlayingRegion);
                        }
                        if (newLandObj == null) //Si no esta completa la region
                        {
                            newLandObj = landRepository.GetLandByGeoHexKey(newLand.GeohexKey);
                        }
                        earthwatcher.Lands = new List<Land>();
                        earthwatcher.Lands.Add(newLandObj);
                        //Comunico a los usuarios conectados si es que la nueva land es de un usuario existente
                        NotificateUsers(newLand, earthwatcher.Id);

                        //Le pasa el UserId y AccessToken para conectarse
                        newEarthwatcher.UserId = ew.UserId;
                        newEarthwatcher.AccessToken = ew.AccessToken;

                        return new HttpResponseMessage<Earthwatcher>(newEarthwatcher) { StatusCode = HttpStatusCode.OK };
                    }
                }
                else
                {
                    //Si ya existe en mi tabla ApiLogin Le updateo el accessToken
                    if(ew.AccessToken != apiEw.AccessToken)
                    {
                        earthwatcherRepository.UpdateAccessToken(apiEw.Id, ew.AccessToken);
                    }
                    //Lo busco por el Id del EW relacionado
                    Earthwatcher earthwatcher = earthwatcherRepository.GetEarthwatcher(apiEw.EarthwatcherId);

                    //Le Agrega el UserId al Earthwatcher y lo devuelve
                    earthwatcher.UserId = apiEw.UserId;
                    earthwatcher.AccessToken = ew.AccessToken;
                    return new HttpResponseMessage<Earthwatcher>(earthwatcher) { StatusCode = HttpStatusCode.OK };
                }
            }
            return new HttpResponseMessage<Earthwatcher>(null) { StatusCode = HttpStatusCode.BadRequest };
        }
        public HttpResponseMessage<Earthwatcher> Post(Earthwatcher earthwatcher, HttpRequestMessage<Earthwatcher> request)
        {
            // quick and easy model validation
            if (String.IsNullOrEmpty(earthwatcher.Name) |
                String.IsNullOrEmpty(earthwatcher.Password) |
                String.IsNullOrEmpty(earthwatcher.Country))
            {
                return new HttpResponseMessage<Earthwatcher>(HttpStatusCode.BadRequest) { ReasonPhrase = "Model is not valid" };
            }

            var earthwatcherDb = earthwatcherRepository.GetEarthwatcher(earthwatcher.Name, false);
            if (earthwatcherDb == null)
            {
                //set localization preferences
                var locSrv = new LocalizationService();
                string locale = locSrv.GetLocale();
                if (locSrv.IsLocaleSetted())
                {
                    earthwatcher.Country = (locale != null) ? locale.Substring(3, 2) : "es-AR";
                    earthwatcher.Language = (locale != null) ? locale.Substring(0, 5) : "en-CA";
                    earthwatcher.PlayingRegion = (locale != null) ? Convert.ToInt32(locale.Substring(6, 1)) : 1;
                    earthwatcher.PlayingCountry = (regionRepository.GetById(earthwatcher.PlayingRegion)).CountryCode;

                }

                earthwatcher.Guid = Guid.NewGuid();
                earthwatcherRepository.CreateEarthwatcher(earthwatcher);
                // assign land

                var newLand = earthwatcherRepository.AssignLandToEarthwatcher(earthwatcher.Id, string.Empty);
                Land newLandObj = null;
                if (newLand == null) //Si la region esta completa le asigno la land del tutor
                {
                    newLandObj = landRepository.GetTutorLand(earthwatcher.PlayingRegion);
                }
                if (newLandObj == null) //Si no esta completa la region
                {
                    newLandObj = landRepository.GetLandByGeoHexKey(newLand.GeohexKey);
                }
                earthwatcher.Lands = new List<Land>();
                earthwatcher.Lands.Add(newLandObj);
                //Comunico a los usuarios conectados si es que la nueva land es de un usuario existente
                NotificateUsers(newLand, earthwatcher.Id);

                //Mando el mail de bienvenida
                try
                {
                    if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["smtp.enabled"]))
                    {
                        List<System.Net.Mail.MailMessage> messages = new List<System.Net.Mail.MailMessage>();

                        System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(earthwatcher.Name);
                        System.Net.Mail.MailAddress addressFrom = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["smtp.user"], "Guardianes - Greenpeace");
                        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                        message.From = addressFrom;
                        message.To.Add(address);
                        message.Subject = Labels.Labels.MailBienvenidaAsunto;

                        string domain = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).GetLeftPart(UriPartial.Authority);

                        string htmlTemplate = System.IO.File.ReadAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mail.html"));
                        message.Body = string.Format(htmlTemplate, Labels.Labels.MailBienvenidaTitulo
                            , Labels.Labels.MailBienvenidaCuerpo
                            , string.Format("{0}/index.html", domain), Labels.Labels.MailBienvenidaBotonTooltip, Labels.Labels.MailBienvenidaBoton, Labels.Labels.LandVerifications6, earthwatcher.Name
                            , Labels.Labels.LandVerifications7
                            , Labels.Labels.LandVerifications8, domain);
                        message.IsBodyHtml = true;
                        message.BodyEncoding = System.Text.Encoding.UTF8;
                        message.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.None;
                        messages.Add(message);
                        SendMails.Send(messages);
                    }

                    return new HttpResponseMessage<Earthwatcher>(earthwatcher) { StatusCode = HttpStatusCode.Created };
                }
                catch (Exception ex)
                {
                    return new HttpResponseMessage<Earthwatcher>(earthwatcher) { StatusCode = HttpStatusCode.InternalServerError, ReasonPhrase = ex.Message };
                }
            }
            throw new HttpResponseException("error: user already exists");
        }
Ejemplo n.º 4
0
 public global()
 {
     _localizationService = new LocalizationService();
 }