Ejemplo n.º 1
0
        public HttpResponseMessage Register(ServiceModel.AddUserModel value)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var existingUser = context.GetAll <DomainModel.User>()
                               .FirstOrDefault(u => u.EmailAddress.Equals(value.EmailAddress, StringComparison.OrdinalIgnoreCase));

            if (existingUser != null)
            {
                ModelState.AddModelError("", "A user with this email address has already registered!");
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var user         = new DomainModel.User(value.EmailAddress);
            var defaultImage = new Uri(Request.RequestUri, "/images/GenericUserImage.gif");

            user.ImagePath = defaultImage.ToString();
            context.Add(user);
            context.SaveChanges();

            var sUser    = user.MapToServiceModel();
            var response = new HttpResponseMessage <ServiceModel.User>(sUser, HttpStatusCode.Created);

            response.Headers.Location = new Uri(Request.RequestUri, "/api/user/" + sUser.Id.ToString());
            return(response);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage AddMember(int id, ServiceModel.AddMemberViewModel viewModel)
        {
            if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson())); }

            //get team
            var team = GetTeam(id);

            var user = context.GetAll<DomainModel.User>()
                              .FirstOrDefault(u => u.EmailAddress == viewModel.EmailAddress);

            if (user == null)
            {
                user = new DomainModel.User(viewModel.EmailAddress);
                context.Add(user);
            }

            if (user.Teams.Any(ut => ut.TeamId == team.Id)) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User already added to team"));

            var newTeamMember = new DomainModel.TeamUser(team, user);
            var inviter = team.Members.FirstOrDefault(x => x.UserId == viewModel.AddedByUserId);

            if (inviter == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User Not Allowed to Invite Members to this Team")); }

            if (team.IsOpen || (inviter != null && inviter.Role== DomainModel.TeamUserRole.Administrator))
            {
                newTeamMember.Status = DomainModel.TeamUserStatus.Approved;
            }

            emailService.InvitedToTeam(user, inviter.User, team).Send();

            team.Members.Add(newTeamMember);
            context.SaveChanges();

            return ResourceOkResponse(team.MapToBasicServiceModel());
        }
Ejemplo n.º 3
0
 public ThingLog(User user, Thing thing)
 {
     this.Thing = thing;
     this.EditedBy = user;
     this.ThingId = thing.Id;
     this.EditedByUserId = user.Id;
     this.DateOccured = DateTime.Now;
 }
Ejemplo n.º 4
0
        public Team(string name, User owner, bool isOpen = false)
            : this()
        {
            this.Name = name;
            this.IsOpen = isOpen;
            this.DateCreated = DateTime.Now;

            ChangeOwner(owner);
        }
Ejemplo n.º 5
0
        public Thing(Team team, User owner)
            : this()
        {
            this.DateCreated = DateTime.Now;
            this.Status = ThingStatus.InProgress;
            this.Team = team;
            this.TeamId = team.Id;

            this.SetOwner(owner);
        }
Ejemplo n.º 6
0
        public HttpResponseMessage OAuth(ServiceModel.OAuthSignInModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
            }

            //validate user
            var    provider = AuthFactory.GetProvider(model.Provider, model.AuthToken);
            var    userInfo = provider.GetUser();
            string userId   = userInfo.UserId;

            if (string.IsNullOrWhiteSpace(userId))
            {
                ModelState.AddModelError("", string.Format("{0} could not locate a user using the provided auth token."));
                return(Request.CreateResponse(HttpStatusCode.Unauthorized, ModelState.ToJson()));
            }

            //get actual user
            var user = context.GetAll <DomainModel.User>()
                       .FirstOrDefault(u => u.OAuthProvider.Equals(model.Provider, StringComparison.OrdinalIgnoreCase) && u.OAuthUserId.Equals(userId, StringComparison.OrdinalIgnoreCase));

            if (user == null)
            {
                //try to find users by existing email address (mostly to clean up v1)
                if (!string.IsNullOrWhiteSpace(userInfo.Email))
                {
                    user = context.GetAll <DomainModel.User>()
                           .FirstOrDefault(u => u.EmailAddress.Equals(userInfo.Email, StringComparison.OrdinalIgnoreCase));
                }

                //user really is new, lets create them
                if (user == null)
                {
                    user = new DomainModel.User(model.Provider, userId);
                    context.Add(user);
                }

                user.EmailAddress = userInfo.Email;
                user.ImagePath    = userInfo.PictureUrl;
                user.FirstName    = userInfo.FirstName;
                user.LastName     = userInfo.LastName;

                if (string.IsNullOrWhiteSpace(user.ImagePath))
                {
                    var defaultImage = new Uri(Request.RequestUri, "/images/GenericUserImage.gif");
                    user.ImagePath = defaultImage.ToString();
                }

                context.SaveChanges();
            }

            //FormsAuthentication.SetAuthCookie(user.EmailAddress, true);
            return(Request.CreateResponse(HttpStatusCode.OK, user.MapToServiceModel()));
        }
Ejemplo n.º 7
0
        public virtual MailMessage DeniedTeam(User user, Team team)
        {
            var mailMessage = new MailMessage { Subject = "TeamThing - Team Access Denied" };

            mailMessage.To.Add(user.EmailAddress);
            mailMessage.From = new MailAddress(fromAddress);
            //ViewBag.Data = someObject;

            ViewData.Model = new TeamAccessChanged { Team = team };
            PopulateBody(mailMessage, viewName: "DeniedTeam");

            return mailMessage;
        }
Ejemplo n.º 8
0
 public static ServiceModel.UserBasic MapToBasicServiceModel(this DomainModel.User user)
 {
     if (user == null)
     {
         return(null);
     }
     return(new ServiceModel.UserBasic()
     {
         Id = user.Id,
         EmailAddress = user.EmailAddress,
         ImagePath = user.ImagePath ?? "/images/GenericUserImage.gif"
     });
 }
Ejemplo n.º 9
0
 public static ServiceModel.UserBasic MapToBasicServiceModel(this DomainModel.User user)
 {
     if (user == null)
     {
         return(null);
     }
     return(new ServiceModel.UserBasic()
     {
         Id = user.Id,
         EmailAddress = user.EmailAddress,
         ImagePath = user.ImagePath ?? "/images/GenericUserImage.gif",
         FirstName = user.FirstName ?? "Team",
         LastName = user.LastName ?? "Member",
         Nickname = (!String.IsNullOrEmpty(user.FirstName) && !String.IsNullOrEmpty(user.LastName)) ? user.FirstName + " " + user.LastName.Substring(0, 1).ToUpper() + "." : "User " + user.Id.ToString() + " (No Name)"
     });
 }
Ejemplo n.º 10
0
 public static ServiceModel.User MapToServiceModel(this DomainModel.User user)
 {
     if (user == null)
     {
         return(null);
     }
     return(new ServiceModel.User()
     {
         Id = user.Id,
         ImagePath = user.ImagePath ?? "/images/GenericUserImage.gif",
         EmailAddress = user.EmailAddress,
         Teams = user.Teams.Where(t => t.Status == DomainModel.TeamUserStatus.Approved).Select(tu => tu.Team).MapToBasicServiceModel().ToList(),
         PendingTeams = user.Teams.Where(t => t.Status == DomainModel.TeamUserStatus.Pending).Select(tu => tu.Team).MapToBasicServiceModel().ToList(),
         Things = user.Things.MapToServiceModel().ToList()
     });
 }
Ejemplo n.º 11
0
        public HttpResponseMessage AddMember(int id, ServiceModel.AddMemberViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
            }

            //get team
            var team = GetTeam(id);

            var user = context.GetAll <DomainModel.User>()
                       .FirstOrDefault(u => u.EmailAddress == viewModel.EmailAddress);

            if (user == null)
            {
                user = new DomainModel.User(viewModel.EmailAddress);
                context.Add(user);
            }

            if (user.Teams.Any(ut => ut.TeamId == team.Id))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User already added to team"));
            }

            var newTeamMember = new DomainModel.TeamUser(team, user);
            var inviter       = team.Members.FirstOrDefault(x => x.UserId == viewModel.AddedByUserId);

            if (inviter == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User Not Allowed to Invite Members to this Team"));
            }

            if (team.IsOpen || (inviter != null && inviter.Role == DomainModel.TeamUserRole.Administrator))
            {
                newTeamMember.Status = DomainModel.TeamUserStatus.Approved;
            }

            emailService.InvitedToTeam(user, inviter.User, team).Send();

            team.Members.Add(newTeamMember);
            context.SaveChanges();

            return(ResourceOkResponse(team.MapToBasicServiceModel()));
        }
Ejemplo n.º 12
0
 public static ServiceModel.User MapToServiceModel(this DomainModel.User user)
 {
     if (user == null)
     {
         return(null);
     }
     return(new ServiceModel.User()
     {
         Id = user.Id,
         ImagePath = user.ImagePath ?? "/images/GenericUserImage.gif",
         EmailAddress = user.EmailAddress,
         Teams = user.Teams.Where(t => t.Status == DomainModel.TeamUserStatus.Approved).Select(tu => tu.Team).MapToBasicServiceModel().ToList(),
         PendingTeams = user.Teams.Where(t => t.Status == DomainModel.TeamUserStatus.Pending).Select(tu => tu.Team).MapToBasicServiceModel().ToList(),
         Things = user.Things.MapToServiceModel().ToList(),
         FirstName = user.FirstName ?? "Team",
         LastName = user.LastName ?? "Member",
         Nickname = (!String.IsNullOrEmpty(user.FirstName) && !String.IsNullOrEmpty(user.LastName)) ? user.FirstName + " " + user.LastName.Substring(0, 1).ToUpper() + "." : "User " + user.Id.ToString() + " (No Name)"
     });
 }
Ejemplo n.º 13
0
        private static void TestContext()
        {
            Console.WriteLine("Running Tests");

            User newUser;
            User newUser2;
            Team newTeam;
            Thing thing;
            using (var context = new TeamThingContext())
            {
                //add some new data
                newUser = new User("*****@*****.**");
                newUser.FirstName = "Josh";
                newUser.LastName = "Holt";
                context.Add(newUser);
                context.SaveChanges();

                //add some new data
                newUser2 = new User("*****@*****.**");
                newUser2.FirstName = "Josh2";
                newUser2.LastName = "Holt2";
                context.Add(newUser2);
                context.SaveChanges();

                newTeam = new Team("Closed Team", newUser, false);
                newTeam.TeamMembers.Add(new TeamUser(newTeam, newUser2));
                context.Add(newTeam);
                context.SaveChanges();

                thing = new Thing(newTeam, newUser);
                thing.Description = "Test Thing";
                thing.AssignedTo.Add(new UserThing(thing, newUser2, newUser));
                thing.AssignedTo.Add(new UserThing(thing, newUser, newUser));

                context.Add(thing);
                context.SaveChanges();
             }
            using (var context = new TeamThingContext())
            {
                newUser = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser.EmailAddress);
                newUser2 = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser2.EmailAddress);
                newTeam = context.GetAll<Team>().FirstOrDefault(p => p.Name == newTeam.Name);
                thing = context.GetAll<Thing>().FirstOrDefault(p => p.Description == thing.Description);
                ////recall the saved items
                //IProduct retrievedProduct = context.Products.FirstOrDefault(p => p.Name == newProduct.Name);
                //ICategory retrievedCategory = context.Categories.FirstOrDefault(p => p.Name == newCategory.Name);
                //Debug.Assert(retrievedProduct != null);
                //Debug.Assert(retrievedCategory!= null);
                //Debug.Assert(retrievedProduct.Category == retrievedCategory);
                //Debug.Assert(retrievedCategory.Products.Contains(retrievedProduct));
                Debug.Assert(newUser != null);
                Debug.Assert(newUser2 != null);
                Debug.Assert(newTeam != null);
                Debug.Assert(thing != null);

                Debug.Assert(thing.Owner != null);
                Debug.Assert(newTeam.Owner != null);

                Debug.Assert(newUser.Teams.Count > 0);

                Debug.Assert(newUser2.Teams.Count > 0);
                Debug.Assert(newUser2.Things.Count > 0);

                Debug.Assert(newTeam.TeamMembers.Count > 0);
                //Debug.Assert(newTeam.TeamThings.Count > 0);

                Debug.Assert(thing.AssignedTo.Count > 0);

            //        //clean up
            //        //Category is marked as dependent, so it will be removed automatically with the product
                context.Delete(thing);
                context.Delete(newTeam);
                context.Delete(newUser);
                context.Delete(newUser2);

                context.SaveChanges();
            }

            Console.WriteLine("All Tests Pass!");
        }
Ejemplo n.º 14
0
 public void ChangeOwner(User newOwner)
 {
     this.Owner = newOwner;
     this.OwnerId = newOwner.Id;
     var teamUser = new TeamUser(this, newOwner);
     teamUser.Status = TeamUserStatus.Approved;
     teamUser.Role = TeamUserRole.Administrator;
     this.Members.Add(teamUser);
 }
Ejemplo n.º 15
0
 public void Delete(User user)
 {
     this.IsDeleted = true;
     this.Status = ThingStatus.Deleted;
     LogChange(user, ThingAction.Deleted);
 }
Ejemplo n.º 16
0
        private static void TestContext()
        {
            Console.WriteLine("Running Tests");

            User newUser;
            User newUser2;
            Team newTeam;
            Thing thing;
            using (var context = new TeamThingContext())
            {
                //add some new data
                newUser = new User("UNIT TEST USER");
                newUser.FirstName = "Josh";
                newUser.LastName = "Holt";
                context.Add(newUser);
                context.SaveChanges();

                //add some new data
                newUser2 = new User("UNIT TEST USER 2");
                newUser2.FirstName = "Josh2";
                newUser2.LastName = "Holt2";
                context.Add(newUser2);
                context.SaveChanges();

                newTeam = new Team("UNIT TEST TEAM", newUser, false);
                newTeam.Members.Add(new TeamUser(newTeam, newUser2));
                context.Add(newTeam);
                context.SaveChanges();

                thing = new Thing(newTeam, newUser);
                thing.Description = "UNIT TEST THING";
                thing.AssignedTo.Add(new UserThing(thing, newUser2, newUser));
                thing.AssignedTo.Add(new UserThing(thing, newUser, newUser));

                context.Add(thing);
                context.SaveChanges();

                thing.UpdateStatus(newUser2, ThingStatus.Delayed);
                thing.Complete(newUser2);

                context.SaveChanges();
            }

            using (var context = new TeamThingContext())
            {
                newUser = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser.EmailAddress);
                newUser2 = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser2.EmailAddress);
                newTeam = context.GetAll<Team>().FirstOrDefault(p => p.Name == newTeam.Name);
                thing = context.GetAll<Thing>().FirstOrDefault(p => p.Description == thing.Description);

                Debug.Assert(newUser != null);
                Debug.Assert(newUser2 != null);
                Debug.Assert(newTeam != null);
                Debug.Assert(thing != null);

                Debug.Assert(thing.Owner != null);
                Debug.Assert(newTeam.Owner != null);

                Debug.Assert(newUser.Teams.Count > 0);

                Debug.Assert(newUser2.Teams.Count > 0);
                Debug.Assert(newUser2.Things.Count > 0);

                Debug.Assert(newTeam.Members.Count > 0);
                //Debug.Assert(newTeam.TeamThings.Count > 0);

                Debug.Assert(thing.AssignedTo.Count > 0);

                Debug.Assert(thing.History.Count == 2);

                Debug.Assert(thing.History[0].EditedBy != null);
                Debug.Assert(thing.History[0].EditedByUserId != 0);

                Debug.Assert(thing.History[0].Thing == thing);
                Debug.Assert(thing.History[0].ThingId == thing.Id);

                //        //clean up
                //        //Category is marked as dependent, so it will be removed automatically with the product
                context.Delete(thing);
                context.Delete(newTeam);
                context.Delete(newUser);
                context.Delete(newUser2);

                context.SaveChanges();
            }

            Console.WriteLine("All Tests Pass!");
        }
Ejemplo n.º 17
0
        public virtual MailMessage InvitedToTeam(User sendTo, User inviter, Team team)
        {
            var mailMessage = new MailMessage { Subject = "TeamThing - Invited to Join Team" };

            mailMessage.To.Add(sendTo.EmailAddress);
            mailMessage.From = new MailAddress(fromAddress);
            //ViewBag.Data = someObject;

            ViewData.Model = new TeamAccessChanged { Team = team };
            PopulateBody(mailMessage, viewName: "InvitedToTeam");

            return mailMessage;
        }
Ejemplo n.º 18
0
 //public void Complete(int userId)
 //{
 //    this.Status = ThingStatus.Completed;
 //    LogStatusChange(userId);
 //}
 //public void UpdateStatus(int userId, ThingStatus status)
 //{
 //    this.Status = status;
 //    LogStatusChange(userId);
 //}
 public void UpdateStatus(User user, ThingStatus status)
 {
     this.Status = status;
     LogStatusChange(user);
 }
Ejemplo n.º 19
0
 private void SetOwner(User newOwner)
 {
     this.Owner = newOwner;
     this.OwnerId = newOwner.Id;
 }
Ejemplo n.º 20
0
        public virtual MailMessage ThingCompleted(User[] assignedTo, User completer, Thing thing)
        {
            var sendTo = assignedTo.Where(u => u.Id != completer.Id).Select(u => u.EmailAddress);

            if (sendTo.Count() > 0)
            {
                var mailMessage = new MailMessage { Subject = "TeamThing - Thing Completed" };
                mailMessage.To.Add(string.Join(",", sendTo));
                mailMessage.From = new MailAddress(fromAddress);
                //ViewBag.Data = someObject;
                ViewData.Model = new ThingChanged { Thing = thing, ChangeMadeBy = completer };
                PopulateBody(mailMessage, viewName: "ThingCompleted");

                return mailMessage;
            }

            return null;
        }
Ejemplo n.º 21
0
        public HttpResponseMessage OAuth(ServiceModel.OAuthSignInModel model)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
            }

            //validate user
            var provider = AuthFactory.GetProvider(model.Provider, model.AuthToken);
            var userInfo = provider.GetUser();
            string userId = userInfo.UserId;

            if (string.IsNullOrWhiteSpace(userId))
            {
                ModelState.AddModelError("", string.Format("{0} could not locate a user using the provided auth token."));
                return Request.CreateResponse(HttpStatusCode.Unauthorized, ModelState.ToJson());
            }

            //get actual user
            var user = context.GetAll<DomainModel.User>()
                              .FirstOrDefault(u => u.OAuthProvider.Equals(model.Provider, StringComparison.OrdinalIgnoreCase) && u.OAuthUserId.Equals(userId, StringComparison.OrdinalIgnoreCase));

            if (user == null)
            {
                //try to find users by existing email address (mostly to clean up v1)
                if (!string.IsNullOrWhiteSpace(userInfo.Email))
                {
                    user = context.GetAll<DomainModel.User>()
                                  .FirstOrDefault(u => u.EmailAddress.Equals(userInfo.Email, StringComparison.OrdinalIgnoreCase));
                }

                //user really is new, lets create them
                if (user == null)
                {
                    user = new DomainModel.User(model.Provider, userId);
                    context.Add(user);
                }

                user.EmailAddress = userInfo.Email;
                user.ImagePath = userInfo.PictureUrl;
                user.FirstName = userInfo.FirstName;
                user.LastName = userInfo.LastName;

                if (string.IsNullOrWhiteSpace(user.ImagePath))
                {
                    var defaultImage = new Uri(Request.RequestUri, "/images/GenericUserImage.gif");
                    user.ImagePath = defaultImage.ToString();
                }

                context.SaveChanges();
            }

            //FormsAuthentication.SetAuthCookie(user.EmailAddress, true);
            return Request.CreateResponse(HttpStatusCode.OK, user.MapToServiceModel());
        }
Ejemplo n.º 22
0
        public HttpResponseMessage Register(ServiceModel.AddUserModel value)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
            }

            var existingUser = context.GetAll<DomainModel.User>()
                                   .FirstOrDefault(u => u.EmailAddress.Equals(value.EmailAddress, StringComparison.OrdinalIgnoreCase));

            if (existingUser != null)
            {
                ModelState.AddModelError("", "A user with this email address has already registered!");
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
            }

            var user = new DomainModel.User(value.EmailAddress);
            var defaultImage = new Uri(Request.RequestUri, "/images/GenericUserImage.gif");
            user.ImagePath = defaultImage.ToString();
            context.Add(user);
            context.SaveChanges();

            var sUser = user.MapToServiceModel();
            var response = Request.CreateResponse(HttpStatusCode.Created, sUser);
            response.Headers.Location = new Uri(Request.RequestUri, "/api/user/" + sUser.Id.ToString());
            return response;
        }
Ejemplo n.º 23
0
 //private void LogChange(int userId, ThingAction action)
 //{
 //    var log = new ThingLog(userId, this.Id);
 //    log.Action = action;
 //    this.History.Add(log);
 //}
 private void LogChange(User user, ThingAction action)
 {
     var log = new ThingLog(user, this);
     log.Action = action;
     this.History.Add(log);
 }
Ejemplo n.º 24
0
 public void ChangeOwner(User user, User newOwner)
 {
     SetOwner(newOwner);
     LogChange(user, ThingAction.OwnerChanged);
 }
Ejemplo n.º 25
0
 //public void Delete(int userId)
 //{
 //    this.IsDeleted = true;
 //    this.Status = ThingStatus.Deleted;
 //    LogChange(userId, ThingAction.Deleted);
 //}
 public void Complete(User user)
 {
     this.Status = ThingStatus.Completed;
     LogStatusChange(user);
 }
Ejemplo n.º 26
0
        public virtual MailMessage ThingUnassigned(User[] unassignedTo, Thing thing, User remover)
        {
            var mailMessage = new MailMessage { Subject = "TeamThing - Something off your plate!" };

            var sendTo = unassignedTo.Select(u => u.EmailAddress);

            if (sendTo.Count() > 0)
            {
                mailMessage.To.Add(string.Join(",", sendTo));
                mailMessage.From = new MailAddress(fromAddress);
                //ViewBag.Data = someObject;
                ViewData.Model = new ThingChanged { Thing = thing, ChangeMadeBy = remover };
                PopulateBody(mailMessage, viewName: "ThingUnassigned");

                return mailMessage;
            }

            return null;
        }
Ejemplo n.º 27
0
 //private void LogStatusChange(int userId)
 //{
 //    LogChange(userId, (this.Status == ThingStatus.Completed) ? ThingAction.Completed : ThingAction.StatusChanged);
 //}
 private void LogStatusChange(User user)
 {
     LogChange(user, (this.Status == ThingStatus.Completed) ? ThingAction.Completed : ThingAction.StatusChanged);
 }