/// <summary>
        /// Remove all of the friend edges associated with a PhotoHunt user.
        /// </summary>
        /// <param name="toRemove">The user whose friend edges will be removed.</param>
        public static void RemoveFriendEdges(User toRemove)
        {
            PhotohuntContext db = new PhotohuntContext();

            // Remove this user's edges.
            var query = from b in db.Edges
                        where b.photohuntUserId.Equals(toRemove.id)
                        select b;
            foreach (var edge in query)
            {
                db.Entry(edge).State = EntityState.Deleted;
            }

            // Remove this user from friend edges.
            var friendEdgeQuery = from b in db.Edges
                                  where b.friendUserId.Equals(toRemove.id)
                                  select b;
            foreach (var edge in friendEdgeQuery)
            {
                db.Entry(edge).State = EntityState.Deleted;
            }

            db.Entry(toRemove).State = EntityState.Deleted;

            // Remove this user's photos
            var photosQuery = from b in db.Photos
                              where b.ownerUserId.Equals(toRemove.id)
                              select b;
            foreach (var photo in photosQuery)
            {
                // Remove votes for this photo.
                var photoVotesQuery = from b in db.Votes
                                      where b.photoId.Equals(photo.id)
                                      select b;
                foreach (var vote in photoVotesQuery)
                {
                    db.Entry(vote).State = EntityState.Deleted;
                }

                // Remove the photo from the DB.
                db.Entry(photo).State = EntityState.Deleted;
            }

            db.SaveChanges();
        }
 /// <summary>
 /// Delete a user's photo.
 /// </summary>
 /// <param name="context">The context containing the request, response, and so on.</param>
 /// <param name="user">The PhotoHunt user deleting the photo.</param>
 /// <param name="photo">The Photo object to be deleted.</param>
 public static void DeletePhoto(HttpContext context, User user, Photo toDelete)
 {
     // User will be NULL if there isn't someone signed in.
     if (user == null || user.id != toDelete.ownerUserId)
     {
         context.Response.StatusCode = 401;
         context.Response.StatusDescription = "Unauthorized request.";
         return;
     }
     PhotohuntContext dbRemove = new PhotohuntContext();
     dbRemove.Entry(toDelete).State = EntityState.Deleted;
     dbRemove.SaveChanges();
 }
Exemple #3
0
        /// <summary>
        /// Either:
        /// 1. Create a user for the given ID and credential
        /// 2. Or, update the existing user with the existing credential
        ///
        /// If 2, then ask Google for the user's public profile information to store.
        /// </summary>
        /// <param name="authState">The OAuth v2 state for authorizing the user.</param>
        /// <returns>A User object that represents the created user.</returns>
        public User SaveTokenForUser(IAuthorizationState authState)
        {
            // Set the auth state in a the superclass for the authorization call.
            _authState = authState;

            var provider = new WebServerClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = CLIENT_ID;
            provider.ClientSecret = CLIENT_SECRET;
            var authenticator =
                new OAuth2Authenticator<WebServerClient>(
                    provider,
                    GetAuthorization)
                {
                    NoCaching = true
                };
            ps = new PlusService(authenticator);

            Person me = ps.People.Get("me").Fetch();

            // Load the user model from the DB if the user already exists.
            bool userExists = false;
            User user = new User();
            PhotohuntContext db = new PhotohuntContext();
            User existing = db.Users.FirstOrDefault(u => u.googleUserId.Equals(me.Id));
            if (existing != null)
            {
                user = existing;
                userExists = true;
            }

            if (!userExists)
            {
                // Save the new user.
                user.googleAccessToken = authState.AccessToken;
                user.googleRefreshToken =
                        authState.RefreshToken == null ? "" : authState.RefreshToken;
                user.googleExpiresIn = (int)(authState.AccessTokenExpirationUtc.Value -
                        authState.AccessTokenIssueDateUtc.Value).TotalSeconds;
                user.googleExpiresAt = authState.AccessTokenExpirationUtc.Value;
                user.googleUserId = me.Id;
                user.googleDisplayName = me.DisplayName;
                user.googlePublicProfilePhotoUrl = me.Image.Url;
                user.googlePublicProfileUrl = me.Url;
                user.email = me.Emails == null ? "" : me.Emails[0].Value;

                db.Users.Add(user);
                db.SaveChanges();
                db.Entry(user);

                // Use the FriendsHelper to generate this user's list of friends
                // who also use this app.
                PhotoHunt.utils.FriendsHelper.GenerateFriends(user, ps);
            }
            else
            {
                // Update the existing user's authorization state. Note that we aren't updating the
                // refresh token because it is only returned the first time the user authorizes the
                // app.
                user.googleAccessToken = authState.AccessToken;
                user.googleExpiresIn = (int)(authState.AccessTokenExpirationUtc.Value -
                        authState.AccessTokenIssueDateUtc.Value).TotalSeconds;
                user.googleExpiresAt = authState.AccessTokenExpirationUtc.Value;
                db.SaveChanges();
            }

            return user;
        }
        /// <summary>
        /// Remove all of a user's votes from the PhotoHunt database.
        /// </summary>
        /// <param name="toRemove">The user whose votes will be removed.</param>
        public static void RemoveVotes(User toRemove)
        {
            PhotohuntContext db = new PhotohuntContext();

            // Remove this user's votes and decrement vote count.
            var votesQuery = from b in db.Votes
                             where b.ownerUserId == toRemove.id
                             select b;
            foreach (var vote in votesQuery)
            {
                PhotohuntContext dbInner = new PhotohuntContext();

                Photo voteTarget = dbInner.Photos.First(p => p.id == vote.photoId);
                voteTarget.numVotes -= 1;
                dbInner.SaveChanges();

                db.Entry(vote).State = EntityState.Deleted;
            }
            db.SaveChanges();
        }