protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;
            var manager = new AuthenticationIdentityManager(new IdentityStore());
            User u = new User(userName) { UserName = userName };
            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                //OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
                {
                    UserDetail details = new UserDetail();
                    context.UserDetails.Add(details);
                    context.SaveChanges();
                    var user = context.AspNetUsers.Find(u.Id);
                    user.UserDetailsId = details.UserDetailsId;
                    context.SaveChanges();
                }

                Response.Redirect("~/Account/UserDetails");
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
 protected void LinkButtonAddComment_Click(object sender, EventArgs e)
 {
     using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
     {
         if (!string.IsNullOrEmpty(this.TextBoxNewComment.Text))
         {
             try
             {
                 Comment comment = new Comment();
                 context.Comments.Add(comment);
                 comment.PostId = this.postId;
                 comment.DateCreated = DateTime.Now;
                 comment.UserId = User.Identity.GetUserId();
                 comment.Text = Server.HtmlEncode(this.TextBoxNewComment.Text);
                 context.SaveChanges();
                 Response.Redirect("UserPost.aspx?postId=" + comment.PostId, false);
             }
             catch (Exception ex)
             {
                 ErrorSuccessNotifier.AddErrorMessage("Comment failed! Server error!");
             }
         }
         else
         {
             ErrorSuccessNotifier.AddErrorMessage("Cannot post empty comment!");
         }
     }
 }
예제 #3
0
        protected void BannUserLinkButton_OnClick(object sender, EventArgs e)
        {
            string userToBannId = Request.Params["userID"];
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();
            try
            {
                using (context)
                {
                    var userToBann = context.AspNetUsers.FirstOrDefault(u => u.Id == userToBannId);
                    if (userToBann == null)
                    {
                        throw new ArgumentException("User does not exist!");
                    }

                    var role = context.AspNetRoles.FirstOrDefault(r => r.Name == "banned");
                    userToBann.AspNetRoles.Clear();
                    userToBann.AspNetRoles.Add(role);
                    context.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage(string.Format("User \"{0}\" banned!", userToBann.UserName));
                }
            }
            catch (Exception exception)
            {
                ErrorSuccessNotifier.AddErrorMessage(exception.Message);
            }
        }
        public IQueryable<UserModel> FriendsGridView_GetData()
        {
            string currentUserId = User.Identity.GetUserId();
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();
            var currentUser = context.AspNetUsers.Include("UserDetail").Include("Friends").
                FirstOrDefault(u => u.Id == currentUserId);

            return currentUser.Friends.AsQueryable().Select(UserModel.FromUser);
        }
예제 #5
0
        //Extracting userId from username
        public string GetUserId(string username)
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();

            var user = context.AspNetUsers.FirstOrDefault(x => x.UserName == username);
            if (user != null)
            {
                return user.Id;
            }
            else
            {
                return "";
            }
        }
예제 #6
0
        //Search by users by name or city
        public IQueryable<object> SearchPeople(string query)
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();

            var result = from users in context.AspNetUsers.Include("UserDetails")
                         where users.UserName.StartsWith(query) || users.UserDetail.City.StartsWith(query)
                         select new UserLink
                         {
                             ID = users.Id,
                             Username = users.UserName,
                             City = users.UserDetail.City
                         };

            return result;
        }
예제 #7
0
        //Populating main Grid with all users data
        public IQueryable<object> GetAllUsersGridData(string userID = null)
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();

            var result = from users in context.AspNetUsers.Include("Posts").Include("UserDetails")
                         where users.Posts.Count > 0
                         select new
                         {
                             ID = users.Id,
                             Username = users.UserName,
                             LatestPost =
                             users.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(),
                             AvatarImage = users.UserDetail.AvatarImage
                         };

            return result;
        }
예제 #8
0
 protected void AcceptFriendRequestLinkButton_OnCommand(object sender, CommandEventArgs e)
 {
     try
     {
         string friendId = e.CommandArgument.ToString();
         SocialNetworkDbEntities contex = new SocialNetworkDbEntities();
         using (contex)
         {
             var friend = contex.AspNetUsers.FirstOrDefault(u => u.Id == friendId);
             var currentUserId = User.Identity.GetUserId();
             var currentUser = contex.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);
             currentUser.Friends.Add(friend);
             contex.SaveChanges();
             ErrorSuccessNotifier.AddSuccessMessage("Friend request accepted!");
         }
     }
     catch (Exception exception)
     {
         ErrorSuccessNotifier.AddErrorMessage(exception);
     }
 }
예제 #9
0
 protected void AddFriendLinkButton_OnCommand(object sender, CommandEventArgs e)
 {
     try
     {
         string userId = Request.Params["userID"];
         SocialNetworkDbEntities context = new SocialNetworkDbEntities();
         using (context)
         {
             var user = context.AspNetUsers.FirstOrDefault(u => u.Id == userId);
             var currentUserId = User.Identity.GetUserId();
             var currentUser = context.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);
             currentUser.Friends.Add(user);
             context.SaveChanges();
             ErrorSuccessNotifier.AddSuccessMessage("Friend request sent!");
             this.AddFriendLinkButton.Visible = false;
         }
     }
     catch (Exception exception)
     {
         ErrorSuccessNotifier.AddErrorMessage(exception);
     }
 }
        protected void ButtonUploadImage_Click(object sender, EventArgs e)
        {
            if (!this.FileUploadControl.HasFile)
            {
                ErrorSuccessNotifier.AddErrorMessage("No image attached.");
                return;
            }
            if(this.FileUploadControl.PostedFile.ContentType == "image/jpeg" ||
                this.FileUploadControl.PostedFile.ContentType == "image/png" ||
                this.FileUploadControl.PostedFile.ContentType == "image/gif")
            {
                int length = FileUploadControl.PostedFile.ContentLength;
                if (length > 819200) // 800Kb = 1024 * 800
                {
                    ErrorSuccessNotifier.AddErrorMessage("The lenght of image is too big. Image must be less than 800kb.");
                    return;
                }

                using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
                {
                    var user = context.AspNetUsers.FirstOrDefault(usr => usr.Id == this.userId);
                    if (user != null)
                    {
                        byte[] fileData = new byte[length + 1];
                        Stream fileStream = FileUploadControl.PostedFile.InputStream;
                        fileStream.Read(fileData, 0, length);

                        user.UserDetail.AvatarImage = fileData;
                        context.SaveChanges();
                        this.profilePicture.Src = "data:image/jpeg;base64," + Convert.ToBase64String(fileData);
                    }
                }
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("The file was not in correct format! Only .jpeg, .png and .gif are supported.");
            }
        }
예제 #11
0
        protected void LinkButtonVote_Click(object sender, EventArgs e)
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();
            using (context)
            {
                var post = context.Posts.Find(this.postId);
                if (post != null && !(post.UsersLiked.Any(usr => usr.Id == User.Identity.GetUserId())))
                {
                    var currentUser = context.AspNetUsers.Find(User.Identity.GetUserId());
                    if (currentUser != null)
                    {
                        try
                        {
                            post.Votes++;
                            post.UsersLiked.Add(currentUser);
                            context.SaveChanges();

                            FillPageContent();
                            this.LinkButtonVote.Visible = false;
                        }
                        catch (Exception ex)
                        {
                            ErrorSuccessNotifier.AddErrorMessage("Voting failed! Server error!");
                        }
                    }
                    else
                    {
                        ErrorSuccessNotifier.AddErrorMessage("User account error! Contact administrator...");
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Error occured while registering your vote!" +
                        Environment.NewLine +
                        "Either post doesn't exist or you have already gave your vote");
                }
            }
        }
        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            if (this.CheckIsValid("Details"))
            {
                using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
                {
                    var user = context.AspNetUsers.Find(userId);
                    var userDetails = user.UserDetail;

                    userDetails.City = TrimWords(TextBoxCity.Text);
                    userDetails.Company = TrimWords(TextBoxCompany.Text);
                    userDetails.Email = TrimWords(TextBoxEmail.Text);
                    if (!string.IsNullOrEmpty(TextBoxBirhtDate.Text))
                    {
                        userDetails.BirthDate = DateTime.Parse(TextBoxBirhtDate.Text);
                    }

                    context.SaveChanges();
                }

                ErrorSuccessNotifier.AddSuccessMessage("Details Succsessfuly changed");
            }
        }
예제 #13
0
        //Populating main Grid with friends data
        public IQueryable<object> GetFriends()
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();

            string userId = User.Identity.GetUserId();

            var curUser = context.AspNetUsers.Where(x => x.Id == userId).FirstOrDefault();

            var result = from users in context.AspNetUsers.Include("Posts").Include("UserDetail")
                         where
                         users.Friends.Select(x => x.Id).Contains(userId)
                         && users.BefriendedBy.Select(x => x.Id).Contains(userId)
                         && users.Posts.Count > 0
                         select new
                         {
                             ID = users.Id,
                             Username = users.UserName,
                             LatestPost = users.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(),
                             AvatarImage = users.UserDetail.AvatarImage
                         };

            return result.OrderBy(x => x.LatestPost.DateCreated);
        }
예제 #14
0
 protected void Post_Command(object sender, CommandEventArgs e)
 {
     using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
     {
         try
         {
             Post post = new Post();
             context.Posts.Add(post);
             post.AuthorId = User.Identity.GetUserId();
             post.DateCreated = DateTime.Now;
             post.Text = Server.HtmlEncode(this.TextBoxNewPostText.Text);
             context.SaveChanges();
             Response.Redirect("UserPost.aspx?postId=" + post.PostId, false);
         }
         catch (Exception ex)
         {
             ErrorSuccessNotifier.AddErrorMessage("Posting failed! Server error!");
         }
     }
 }
예제 #15
0
        private void FillForeignProfile(string userId)
        {
            var context = new SocialNetworkDbEntities();
            using (context)
            {
                try
                {
                    var currentUserId = User.Identity.GetUserId();
                    var currentUser = context.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);

                    var user = context.AspNetUsers.Include("Posts").FirstOrDefault(usr => usr.Id == userId);
                    if (user != null)
                    {
                        this.ProfileImage.ImageUrl = GetImage(user);
                        this.UsernameLiteral.InnerText = Server.HtmlEncode(user.UserName);

                        if (user.Friends.Any(u => u.Id == currentUserId))
                        {
                            this.EmailLiteral.Text += Server.HtmlEncode(user.UserDetail.Email);
                            this.BirthDateLiteral.Text += string.Format("{0:dd-MMMM-yyyy}", user.UserDetail.BirthDate);
                            this.CityLiteral.Text += Server.HtmlEncode(user.UserDetail.City);
                            this.CompanyLiteral.Text += Server.HtmlEncode(user.UserDetail.Company);
                            this.PostsListView.DataSource = user.Posts.OrderByDescending(post => post.DateCreated).Take(5) ;
                            this.PostsListView.DataBind();
                            this.AddFriendLinkButton.Visible = false;
                            this.BackToHomeLinkButton.Visible = false;
                        }
                        else
                        {
                            bool isFriend = CheckIfFriend(userId);
                            if (isFriend)
                            {
                                this.AddFriendLinkButton.Visible = false;
                            }
                            else
                            {
                                this.AddFriendLinkButton.Visible = true;
                            }

                            this.VisibleInfoPanel.Visible = false;
                            this.BackToHomeLinkButton.Visible = true;
                        }
                    }
                    else
                    {
                        throw new ArgumentException("User does not exist!");
                    }
                }
                catch (Exception e)
                {
                    this.AddFriendPanel.Visible = false;
                    this.AdminPanel.Visible = false;
                    this.VisibleInfoPanel.Visible = false;
                    this.ProfileHeaderPanel.Visible = false;
                    this.BackToHomeLinkButton.Visible = true;
                    ErrorSuccessNotifier.AddErrorMessage(e);
                }
            }
        }
예제 #16
0
        private void FillCurrentProfileData()
        {
            var context = new SocialNetworkDbEntities();
            using (context)
            {
                string currentUserId = User.Identity.GetUserId();
                var user = context.AspNetUsers.Include("Posts").FirstOrDefault(usr => usr.Id == currentUserId);

                if (user == null)
                {
                    Response.Redirect("~");
                }

                this.ProfileImage.ImageUrl = GetImage(user);
                this.UsernameLiteral.InnerText = Server.HtmlEncode(user.UserName);
                this.ShowFriendRequestsButton.Visible = true;
                this.FriendRequestListView.Visible = false;
                this.EmailLiteral.Text += Server.HtmlEncode(user.UserDetail.Email);
                this.BirthDateLiteral.Text += string.Format("{0:dd-MMMM-yyyy}", user.UserDetail.BirthDate);
                this.CityLiteral.Text += Server.HtmlEncode(user.UserDetail.City);
                this.CompanyLiteral.Text += Server.HtmlEncode(user.UserDetail.Company);
                this.PostsListView.DataSource = user.Posts.OrderByDescending(post => post.DateCreated).Take(5);
                this.PostsListView.DataBind();
                this.AddFriendLinkButton.Visible = false;
            }
        }
예제 #17
0
        private bool CheckIfFriend(string userId)
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();
            using (context)
            {
                var currentUserId = User.Identity.GetUserId();
                var currentUser = context.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId);

                if (currentUser == null)
                {
                    return false;
                }

                return currentUser.Friends.Any(f => f.Id == userId);
            }
        }
예제 #18
0
        protected void ShowFriendRequestsButton_OnClick(object sender, EventArgs e)
        {
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();
            using (context)
            {
                if (!this.FriendRequestListView.Visible)
                {
                    var currentUserId = User.Identity.GetUserId();
                    var currentUser = context.AspNetUsers.Include("Friends").Include("BefriendedBy").FirstOrDefault(u => u.Id == currentUserId);
                    var befriendedByList = currentUser.BefriendedBy;
                    List<AspNetUser> friendCandidates = new List<AspNetUser>();
                    foreach (var candidate in befriendedByList)
                    {
                        if (!currentUser.Friends.Contains(candidate))
                        {
                            friendCandidates.Add(candidate);
                        }
                    }

                    this.FriendRequestListView.DataSource = friendCandidates;
                    this.FriendRequestListView.DataBind();
                    this.FriendRequestListView.Visible = true;
                }
                else
                {
                    this.FriendRequestListView.Visible = false;
                }
            }
        }
        protected void Page_Prerender(object sender, EventArgs e)
        {
            if (!User.IsInRole("banned"))
            {
                using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
                {
                    var user = context.AspNetUsers.FirstOrDefault(usr => usr.Id == this.userId);
                    if (user != null)
                    {
                        if (this.isInputValidated)
                        {

                            this.LiteralUsername.Text = user.UserName;
                            this.TextBoxCity.Text = user.UserDetail.City;
                            this.TextBoxCompany.Text = user.UserDetail.Company;
                            this.TextBoxEmail.Text = user.UserDetail.Email;
                            var dateAsString = String.Format("{0:dd.MM.yyyy}", user.UserDetail.BirthDate);
                            this.TextBoxBirhtDate.Text = dateAsString;
                            if (user.UserDetail.AvatarImage != null)
                            {
                                this.profilePicture.Src = "data:image/jpeg;base64," +
                                                          Convert.ToBase64String(user.UserDetail.AvatarImage);
                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("~/");
                    }
                }
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("Banned!");
            }
        }
예제 #20
0
        private void FillPageContent()
        {
            if (!isNewPost)
            {
                using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
                {
                    Post post = context.Posts.Include("Comments").Include("AspNetUser").
                        FirstOrDefault(p => p.PostId == this.postId);

                    this.HeaderUsername.InnerText = "Post by " + post.AspNetUser.UserName +
                        " - " + post.Votes.ToString() + " votes";
                    this.TextBoxPostText.Text = post.Text;
                    this.RepeaterComments.DataSource = post.Comments.OrderByDescending(cmnt => cmnt.DateCreated).ToList();
                    this.RepeaterComments.DataBind();

                    this.ViewPostPanel.Visible = true;
                    if (post.UsersLiked.Any(usr => usr.Id == User.Identity.GetUserId()))
                    {
                        LinkButtonVote.Visible = false;
                    }
                }
            }
            else
            {
                this.AddPostPanel.Visible = true;
            }
        }
예제 #21
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load statistics data
            SocialNetworkDbEntities context = new SocialNetworkDbEntities();

            this.LabelTotalUsers.Text = context.AspNetUsers.Count().ToString();
            this.LabelTotalPosts.Text = context.Posts.Count().ToString();
            this.LabelTotalComments.Text = context.Comments.Count().ToString();

            //Grid filtering for logged users
            if (User.Identity.IsAuthenticated)
            {
                this.ButtonShowAllUsers.Visible = true;
                this.ButtonShowFriends.Visible = true;
                this.SearchPeopleButton.Visible = true;
            }
        }
예제 #22
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.IsInRole("banned"))
            {
                this.UserPostPanel.Visible = false;
                ErrorSuccessNotifier.AddErrorMessage("Banned!");
            }
            else
            {
                this.postId =
                    Convert.ToInt32(Request.Params["postId"]);
                this.isNewPost = (this.postId == 0);

                if (this.isNewPost)
                {
                    this.userId = User.Identity.GetUserId();
                    FillPageContent();
                }
                else
                {
                    SocialNetworkDbEntities context = new SocialNetworkDbEntities();
                    var post = context.Posts.Find(this.postId);
                    if (post != null)
                    {
                        if (post.AspNetUser.Friends.Any(usr => usr.Id == User.Identity.GetUserId()) ||
                            post.AspNetUser.Id == User.Identity.GetUserId())
                        {
                            FillPageContent();
                        }
                        else
                        {
                            ErrorSuccessNotifier.AddErrorMessage("You can view posts only by your friends");
                        }
                    }
                    else
                    {
                        ErrorSuccessNotifier.AddErrorMessage("Post not found");
                    }
                }
            }
        }