Пример #1
0
        public IHttpActionResult Delete(int issueId)
        {
            if (issueId <= 0)
            {
                return(BadRequest("Gimmick ID is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    Issue ix = ctx.Issues.SingleOrDefault(x => x.IssueId == issueId);
                    if (ix == null)
                    {
                        return(InternalServerError(new InvalidOperationException("Invalid Issue ID!")));
                    }

                    ctx.Issues.Remove(ix);

                    ctx.SaveChanges();

                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #2
0
        public IHttpActionResult ConfirmUser(string ticket)
        {
            if (string.IsNullOrWhiteSpace(ticket))
            {
                return(BadRequest("The ticket is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    User user = ctx.Users.SingleOrDefault(x => x.Ticket == ticket);
                    if (user == null)
                    {
                        return(BadRequest("This ticket is not a real!"));
                    }

                    user.IsConfirmed = true;
                    ctx.SaveChanges();

                    IHttpActionResult response;
                    //we want a 303 with the ability to set location
                    HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
                    responseMsg.Headers.Location = new Uri("http://www.admaiorastudio.com/bugghy");
                    response = ResponseMessage(responseMsg);
                    return(response);
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #3
0
        public async Task <IHttpActionResult> RegisterUser(Poco.User credentials)
        {
            if (string.IsNullOrWhiteSpace(credentials.Email))
            {
                return(BadRequest("The email is not valid!"));
            }

            if (string.IsNullOrWhiteSpace(credentials.Password))
            {
                return(BadRequest("The password is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    User user = ctx.Users.SingleOrDefault(x => x.Email == credentials.Email);
                    if (user != null)
                    {
                        return(InternalServerError(new InvalidOperationException("This email has already taken!")));
                    }

                    user = new User {
                        Email = credentials.Email, Password = credentials.Password
                    };
                    user.Ticket = Guid.NewGuid().ToString();
                    ctx.Users.Add(user);
                    ctx.SaveChanges();

                    string            apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
                    SendGridAPIClient mc     = new SendGridAPIClient(apiKey);

                    Email   to      = new Email(user.Email);
                    Email   from    = new Email("*****@*****.**");
                    string  subject = "Welocme to Bugghy!";
                    Content content = new Content("text/plain",
                                                  String.Format("Hi {0},\n\nYou registration on Bugghy is almost complete. Please click on this link to confirm your registration!\n\n{1}",
                                                                user.Email.Split('@')[0],
                                                                String.Format("https://bugghy-api.azurewebsites.net/users/confirm?ticket={0}", user.Ticket)));
                    Mail mail = new Mail(from, subject, to, content);

                    dynamic response = await mc.client.mail.send.post(requestBody : mail.Get());

                    return(Ok(Dto.Wrap(new Poco.User
                    {
                        UserId = user.UserId,
                        Email = user.Email,
                        AuthAccessToken = null,
                        AuthExpirationDate = null
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #4
0
        public IHttpActionResult Update(Poco.Issue item)
        {
            if (item.IssueId <= 0)
            {
                return(BadRequest("Issue ID is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Title))
            {
                return(BadRequest("Title is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Description))
            {
                return(BadRequest("Description is not valid!"));
            }
            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    Issue ix = ctx.Issues.SingleOrDefault(x => x.IssueId == item.IssueId);
                    if (ix == null)
                    {
                        return(InternalServerError(new InvalidOperationException("Invalid Issue ID!")));
                    }

                    ix.Title       = item.Title;
                    ix.Description = item.Description;

                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.Issue
                    {
                        IssueId = ix.IssueId,
                        GimmickId = ix.GimmickId,
                        UserId = ix.UserId,
                        Sender = ix.Sender,
                        Code = ix.Code,
                        Title = ix.Title,
                        Description = ix.Description,
                        Type = ix.Type,
                        Status = ix.Status,
                        CreationDate = ix.CreationDate,
                        ReplyDate = ix.ReplyDate,
                        ClosedDate = ix.ClosedDate,
                        IsClosed = ix.IsClosed
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #5
0
        public IHttpActionResult Update(Poco.Gimmick item)
        {
            if (item.GimmickId <= 0)
            {
                return(BadRequest("Gimmick ID is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Name))
            {
                return(BadRequest("Name is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Owner))
            {
                return(BadRequest("Owner is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    Gimmick gi = ctx.Gimmicks.SingleOrDefault(x => x.GimmickId == item.GimmickId);
                    if (gi == null)
                    {
                        return(InternalServerError(new InvalidOperationException("Invalid Gimmick ID!")));
                    }

                    gi.Name        = item.Name;
                    gi.Description = item.Description;
                    gi.Owner       = item.Owner;

                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.Gimmick
                    {
                        GimmickId = gi.GimmickId,
                        Name = gi.Name,
                        Description = gi.Description,
                        Owner = gi.Owner,
                        ImageUrl = gi.ImageUrl,
                        CreationDate = gi.CreationDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #6
0
        public IHttpActionResult AddNew(Poco.Gimmick item)
        {
            if (String.IsNullOrWhiteSpace(item.Name))
            {
                return(BadRequest("Name is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Owner))
            {
                return(BadRequest("Owner is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    Gimmick gi = new Gimmick
                    {
                        Name         = item.Name,
                        Description  = item.Description,
                        Owner        = item.Owner,
                        CreationDate = DateTime.Now.ToUniversalTime()
                    };

                    ctx.Gimmicks.Add(gi);

                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.Gimmick
                    {
                        GimmickId = gi.GimmickId,
                        Name = gi.Name,
                        Description = gi.Description,
                        Owner = gi.Owner,
                        ImageUrl = gi.ImageUrl,
                        CreationDate = gi.CreationDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #7
0
        public IHttpActionResult RestoreUser(string accessToken)
        {
            if (string.IsNullOrWhiteSpace(accessToken))
            {
                return(BadRequest("The access token is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    User user = ctx.Users.SingleOrDefault(x => x.AuthAccessToken == accessToken);
                    if (user == null)
                    {
                        return(Unauthorized());
                    }

                    user.LoginDate      = DateTime.Now.ToUniversalTime();
                    user.LastActiveDate = user.LoginDate;
                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.User
                    {
                        UserId = user.UserId,
                        Email = user.Email,
                        LoginDate = user.LoginDate,
                        AuthAccessToken = user.AuthAccessToken,
                        AuthExpirationDate = user.AuthExpirationDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #8
0
        public async Task <IHttpActionResult> LoginUser(Google.Credentials credentials)
        {
            if (string.IsNullOrWhiteSpace(credentials.ClientID))
            {
                return(BadRequest("The Google client ID is not valid!"));
            }

            if (string.IsNullOrWhiteSpace(credentials.Email))
            {
                return(BadRequest("The email is not valid!"));
            }

            if (string.IsNullOrWhiteSpace(credentials.Token))
            {
                return(BadRequest("The Google token is not valid!"));
            }

            try
            {
                RestClient c = new RestClient(new Uri("https://www.googleapis.com"));

                // To login via google token, we need first to validate the token passed
                // To validate the token we must check if it belongs to our Google application
                // Reference: https://developers.google.com/identity/sign-in/android/backend-auth

                // Validation request
                RestRequest vr = new RestRequest("oauth2/v3/tokeninfo", Method.GET);
                vr.AddParameter("id_token", credentials.Token);
                var r = await c.ExecuteTaskAsync <Google.TokenClaims>(vr);

                if (r.StatusCode != HttpStatusCode.OK)
                {
                    return(InternalServerError(new InvalidOperationException("Unable to login via Google")));
                }

                if (r.Data.aud != credentials.ClientID ||
                    r.Data.email != credentials.Email ||
                    r.Data.email_verified == false)
                {
                    return(InternalServerError(new InvalidOperationException("Unable to login via Google")));
                }

                using (var ctx = new BugghyDbContext())
                {
                    // Check if we have already registered the user, if not this login method will take care of it
                    User user = ctx.Users.SingleOrDefault(x => x.Email == credentials.Email);
                    if (user == null)
                    {
                        user = new User
                        {
                            GoogleId    = r.Data.sub,
                            Email       = credentials.Email,
                            Password    = null,
                            Ticket      = Guid.NewGuid().ToString(),
                            IsConfirmed = true
                        };

                        ctx.Users.Add(user);
                        ctx.SaveChanges();
                    }
                    else
                    {
                        user.GoogleId    = r.Data.sub;
                        user.IsConfirmed = true;

                        ctx.SaveChanges();
                    }

                    var token = GetAuthenticationTokenForUser(user.Email);
                    user.LoginDate          = DateTime.Now.ToUniversalTime();
                    user.LastActiveDate     = user.LoginDate;
                    user.AuthAccessToken    = token.RawData;
                    user.AuthExpirationDate = token.ValidTo;
                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.User
                    {
                        UserId = user.UserId,
                        Email = user.Email,
                        LoginDate = user.LoginDate,
                        AuthAccessToken = user.AuthAccessToken,
                        AuthExpirationDate = user.AuthExpirationDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #9
0
        public IHttpActionResult LoginUser(Poco.User credentials)
        {
            if (string.IsNullOrWhiteSpace(credentials.Email))
            {
                return(BadRequest("The email is not valid!"));
            }

            if (string.IsNullOrWhiteSpace(credentials.Password))
            {
                return(BadRequest("The password is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    User user = ctx.Users.SingleOrDefault(x => x.Email == credentials.Email);
                    if (user == null)
                    {
                        return(Unauthorized());
                    }

                    if (!user.IsConfirmed)
                    {
                        return(InternalServerError(new InvalidOperationException("You must confirm your email first!")));
                    }

                    if (!String.IsNullOrWhiteSpace(user.GoogleId) && user.Password == null)
                    {
                        return(InternalServerError(new InvalidOperationException("You must login via Google!")));
                    }

                    string p1 = FormsAuthentication.HashPasswordForStoringInConfigFile(user.Password, "MD5");
                    string p2 = FormsAuthentication.HashPasswordForStoringInConfigFile(credentials.Password, "MD5");
                    if (p1 != p2)
                    {
                        return(Unauthorized());
                    }

                    var token = GetAuthenticationTokenForUser(user.Email);
                    user.LoginDate          = DateTime.Now.ToUniversalTime();
                    user.LastActiveDate     = user.LoginDate;
                    user.AuthAccessToken    = token.RawData;
                    user.AuthExpirationDate = token.ValidTo;
                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.User
                    {
                        UserId = user.UserId,
                        Email = user.Email,
                        LoginDate = user.LoginDate,
                        AuthAccessToken = user.AuthAccessToken,
                        AuthExpirationDate = user.AuthExpirationDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #10
0
        public IHttpActionResult Post(Poco.Message item)
        {
            if (item.IssueId <= 0)
            {
                return(BadRequest("Issue ID is not valid!"));
            }

            if (item.UserId <= 0)
            {
                return(BadRequest("User ID is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Content))
            {
                return(BadRequest("Title is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    User user = ctx.Users.SingleOrDefault(x => x.UserId == item.UserId);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Unable to find an user with the ID specified!");
                    }

                    Issue issue = ctx.Issues.SingleOrDefault(x => x.IssueId == item.IssueId);
                    if (issue == null)
                    {
                        throw new InvalidOperationException("Unable to find an issue with the ID specified!");
                    }

                    // Update reply date
                    issue.ReplyDate = DateTime.Now.ToUniversalTime();

                    Message me = new Message
                    {
                        IssueId  = item.IssueId,
                        UserId   = item.UserId,
                        Sender   = user.Email,
                        Content  = item.Content,
                        PostDate = DateTime.Now.ToUniversalTime()
                    };

                    ctx.Messages.Add(me);

                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.Message
                    {
                        MessageId = me.MessageId,
                        IssueId = me.IssueId,
                        UserId = me.UserId,
                        Sender = me.Sender,
                        Content = me.Content,
                        PostDate = me.PostDate
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Пример #11
0
        public IHttpActionResult AddNew(Poco.Issue item)
        {
            if (item.GimmickId <= 0)
            {
                return(BadRequest("Gimmick ID is not valid!"));
            }

            if (item.UserId <= 0)
            {
                return(BadRequest("User ID is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Title))
            {
                return(BadRequest("Title is not valid!"));
            }

            if (String.IsNullOrWhiteSpace(item.Description))
            {
                return(BadRequest("Description is not valid!"));
            }

            try
            {
                using (var ctx = new BugghyDbContext())
                {
                    User user = ctx.Users.SingleOrDefault(x => x.UserId == item.UserId);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Unable to find an user with the ID specified!");
                    }

                    Issue ix = new Issue
                    {
                        GimmickId    = item.GimmickId,
                        UserId       = item.UserId,
                        Sender       = user.Email,
                        Title        = item.Title,
                        Description  = item.Description,
                        Type         = item.Type,
                        Status       = IssueStatus.Opened,
                        CreationDate = DateTime.Now.ToUniversalTime()
                    };

                    ctx.Issues.Add(ix);
                    ctx.SaveChanges();

                    // Updating code
                    ix.Code = String.Concat(
                        ix.Type.ToString().Substring(0, 1), "-", ix.IssueId.ToString());
                    ctx.SaveChanges();

                    return(Ok(Dto.Wrap(new Poco.Issue
                    {
                        IssueId = ix.IssueId,
                        GimmickId = ix.GimmickId,
                        UserId = ix.UserId,
                        Sender = ix.Sender,
                        Code = ix.Code,
                        Title = ix.Title,
                        Description = ix.Description,
                        Type = ix.Type,
                        Status = ix.Status,
                        CreationDate = ix.CreationDate,
                        ReplyDate = ix.ReplyDate,
                        ClosedDate = ix.ClosedDate,
                        IsClosed = ix.IsClosed
                    })));
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }