Example #1
0
    public IHttpActionResult Own()
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            User user = new UserContext().Get(userKey);

            var events = EventContext.Own(user.Id);

            if (events != null)
            {
                return(Ok(events));
            }
            else
            {
                return(BadRequest());
            }
        }

        return(Ok("Unauthorized"));
    }
Example #2
0
    public IHttpActionResult Delete(int eventId)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            User user;

            using (UserContext userContext = new UserContext())
            {
                bool retorno = false;
                user = userContext.Get(userKey);

                retorno = EventContext.Delete(eventId, user.Id);

                if (retorno)
                {
                    return(Ok(retorno));
                }
                else
                {
                    return(BadRequest());
                }
            }
        }

        return(Ok("Unauthorized"));
    }
Example #3
0
    public IHttpActionResult Get()
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;
        Dare   dare;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            dare = DareContext.GetDare();
            return(Ok(new
            {
                Dare = dare.DareText,
                dare.Description,
                dare.Shots,
                dare.Points
            }));
        }
        else
        {
            return(Ok("Unauthorized"));
        }
    }
Example #4
0
    public IHttpActionResult Get(int id)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;
        User   user;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            user = UserContext.Get(id);

            return(Ok(new
            {
                user.Id,
                user.DisplayName,
                user.IsUser
            }));
        }

        return(Ok("Unauthorized"));
    }
Example #5
0
    public IHttpActionResult Get(int id)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;
        User   user;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            user = UserContext.Get(id);

            if (user != null)
            {
                return(Ok(new
                {
                    user.Id,
                    user.FirstName,
                    user.LastName,
                    user.ImageURL,
                    user.Status,
                    user.ThemeId
                }));
            }
            else
            {
                BadRequest();
            }
        }

        return(Ok("Unauthorized"));
    }
Example #6
0
    public IHttpActionResult Post(JObject json)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            string name        = json["Name"]?.ToString();
            string address     = json["Address"]?.ToString();
            string description = json["Description"]?.ToString();

            if (!int.TryParse(json["EventId"]?.ToString(), out int eventId))
            {
                eventId = 0;
            }

            if (!int.TryParse(json["TypeId"]?.ToString(), out int typeId) ||
                !double.TryParse(json["Longitude"]?.ToString(), out double longitude) ||
                !double.TryParse(json["Latitude"]?.ToString(), out double latitude) ||
                !DateTime.TryParse(json["Date"]?.ToString(), out DateTime date) ||
                string.IsNullOrEmpty(name) &&
                string.IsNullOrEmpty(address) &&
                string.IsNullOrEmpty(description))
            {
                return(BadRequest());
            }
            else
            {
                User user;

                using (UserContext userContext = new UserContext())
                {
                    Event getEvent;
                    user = userContext.Get(userKey);

                    if (eventId == 0)
                    {
                        getEvent = EventContext.Create(typeId, user.Id, name, description, address, date, longitude, latitude);
                    }
                    else
                    {
                        getEvent = EventContext.Update(eventId, typeId, user.Id, name, description, address, date, longitude, latitude);
                    }

                    if (getEvent != null)
                    {
                        return(Ok(getEvent));
                    }
                }
            }
        }

        return(Ok("Unauthorized"));
    }
Example #7
0
    public IHttpActionResult Participants(int eventId)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth && eventId > 0)
        {
            return(Ok(EventContext.Participants(eventId)));
        }

        return(Ok("Unauthorized"));
    }
Example #8
0
    public IHttpActionResult GetTypes()
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            List <EventType> eventTypes = EventContext.GetTypes();

            return(Ok(eventTypes));
        }

        return(Ok("Unauthorized"));
    }
Example #9
0
    public IHttpActionResult Delete()
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;
        User   user;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            user = UserContext.Get(userKey);

            return(Ok(UserContext.Delete(user.Id)));
        }

        return(Ok("Unauthorized"));
    }
        private async Task <AuthenticateResult> AuthenticateUser(string username, string password)
        {
            var user = await _unitOfWork.UserManager.GetByUsername(username, Request.HttpContext.RequestAborted);

            if (_userContext.Authenticate(user, password))
            {
                var claims = new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                    new Claim(ClaimTypes.Name, user.UserName),
                };
                var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, Scheme.Name));
                var ticket    = new AuthenticationTicket(principal, Scheme.Name);

                return(AuthenticateResult.Success(ticket));
            }
            else
            {
                return(AuthenticateResult.Fail($"Invalid {HeaderKey} Header"));
            }
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey(HeaderKey))
            {
                return(AuthenticateResult.Fail($"Missing {HeaderKey} Header"));
            }

            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers[HeaderKey]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(':');
                var username        = credentials[0];
                var password        = credentials[1];

                var user = await _unitOfWork.UserManager.GetByUsername(username, Request.HttpContext.RequestAborted);

                if (_userContext.Authenticate(user, password))
                {
                    var claims = new[] {
                        new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                        new Claim(ClaimTypes.Name, user.UserName),
                    };
                    var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, Scheme.Name));
                    var ticket    = new AuthenticationTicket(principal, Scheme.Name);

                    return(AuthenticateResult.Success(ticket));
                }
                else
                {
                    return(AuthenticateResult.Fail($"Invalid {HeaderKey} Header"));
                }
            }
            catch (Exception ex)
            {
                return(AuthenticateResult.Fail(ex.Message));
            }
        }
Example #12
0
    public IHttpActionResult Filter(JObject json)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            string          name     = json["Name"]?.ToString();
            Location        location = JsonConvert.DeserializeObject <Location>(json["Location"]?.ToString());
            List <int>      types    = JsonConvert.DeserializeObject <List <int> >(json["Types"] != null ? json["Types"].ToString() : "");
            List <DateTime> dates    = JsonConvert.DeserializeObject <List <DateTime> >(json["Dates"] != null ? json["Dates"].ToString() : "");
            int             radius;

            int.TryParse(json["Radius"]?.ToString(), out radius);

            radius = radius == 0 ? 2000 : radius;


            if (location == null)
            {
                return(BadRequest());
            }
            else
            {
                var eventsFiltered = EventContext.GetFiltered(location, radius, name, types, dates);

                return(Ok(eventsFiltered));
            }
        }

        return(Ok("Unauthorized"));
    }
Example #13
0
    public IHttpActionResult Image()
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;
        User   user;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            var httpRequest = HttpContext.Current.Request;
            var postedFile  = httpRequest.Files.Count > 0 ? httpRequest.Files[0] : null;

            if (postedFile != null && postedFile.ContentLength > 0)
            {
                IList <string> AllowedFileExtensions = new List <string> {
                    ".jpg", ".gif", ".png"
                };
                var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                var extension = ext.ToLower();
                if (!AllowedFileExtensions.Contains(extension))
                {
                    var message = string.Format("Please Upload image of type .jpg,.gif,.png.");

                    return(Ok(new
                    {
                        Success = false,
                        Message = message
                    }));
                }
                else
                {
                    user = UserContext.Get(userKey);

                    var response = UserContext.UploadUserPhoto(postedFile, user.ImageURL);

                    if (response)
                    {
                        return(Ok(new
                        {
                            Success = true,
                            user.ImageURL
                        }));
                    }
                    else
                    {
                        return(Ok(new
                        {
                            Success = false
                        }));
                    }
                }
            }
            else
            {
                return(Ok(new
                {
                    Success = false,
                    Message = "No files sent"
                }));
            }
        }

        return(Ok("Unauthorized"));
    }
Example #14
0
    public IHttpActionResult Update(JObject json)
    {
        string userKey = this.Request.Headers.GetValues("uk").FirstOrDefault();
        bool   auth    = false;
        User   user;

        using (UserContext userContext = new UserContext())
        {
            auth = userContext.Authenticate(userKey);
        }

        if (auth)
        {
            user = UserContext.Get(userKey);

            var firstName = json["FirstName"]?.ToString();
            var lastName  = json["LastName"]?.ToString();
            var status    = json["Status"]?.ToString();
            int.TryParse(json["Theme"]?.ToString(), out int themeId);
            Location home = JsonConvert.DeserializeObject <Location>(json["Home"] != null ? json["Home"].ToString() : "");
            Location work = JsonConvert.DeserializeObject <Location>(json["Work"] != null ? json["Work"].ToString() : "");

            bool success = false;

            success = UserContext.Update(user.Id, firstName, lastName, status, themeId, home, work);

            if (success)
            {
                var locations = UserContext.Getlocations(user.Id);


                if (locations.Count() > 1)
                {
                    return(Ok(new
                    {
                        Success = success,
                        User = new
                        {
                            user.Id,
                            user.FirstName,
                            user.LastName,
                            Home = new
                            {
                                locations.First().Latitude,
                                locations.First().Longitude
                            },
                            Work = new
                            {
                                locations.Last().Latitude,
                                locations.Last().Longitude
                            },
                            user.ImageURL,
                            user.Status,
                            user.ThemeId
                        }
                    }));
                }
                else
                {
                    return(Ok(new
                    {
                        Success = success,
                        User = new
                        {
                            user.Id,
                            user.FirstName,
                            user.LastName,
                            Home = new
                            {
                                locations.First().Latitude,
                                locations.First().Longitude
                            },
                            user.ImageURL,
                            user.Status,
                            user.ThemeId
                        }
                    }));
                }
            }
            else
            {
                return(Ok(new
                {
                    Success = success
                }));
            }
        }

        return(Ok("Unauthorized"));
    }
Example #15
0
 public Users Login(Users user)
 {
     using UserContext context = new UserContext();
     return(context.Authenticate(user));
 }