示例#1
0
        public async Task <IActionResult> OnGetAsync()
        {
            AvatarUrl = "";
            string githubId = "";
            string name     = "";
            string username = "";

            foreach (Claim claim in User.Claims)
            {
                if (claim.Type == "urn:github:avatar")
                {
                    AvatarUrl = claim.Value;
                }
                else if (claim.Type == ClaimTypes.NameIdentifier)
                {
                    githubId = claim.Value;
                }
                else if (claim.Type == ClaimTypes.Name)
                {
                    name = claim.Value;
                }
                else if (claim.Type == "urn:github:login")
                {
                    username = claim.Value;
                }
            }

            ThisUser = await _context.GetUserByGithubId(githubId);

            int userId;

            if (ThisUser != null)
            {
                userId = ThisUser.Id;
            }
            else
            {
                ThisUser = new User
                {
                    Username   = username,
                    Name       = name,
                    GithubId   = githubId,
                    CreateDate = DateTime.Now,
                    AvatarUrl  = AvatarUrl
                };

                var dbResponse = _context.AddUser(ThisUser);
                await _context.SaveChangesAsync();

                userId = dbResponse.Entity.Id;
            }

            UserEntity = new EditUserModel {
                Bio        = ThisUser.Bio,
                ClippyMode = ThisUser.ClippyMode
            };

            return(Page());
        }
示例#2
0
        public async void OnGetAsync()
        {
            DateTime now       = DateTime.Now;
            string   githubId  = "";
            string   name      = "";
            string   username  = "";
            string   avatarUrl = "";

            foreach (Claim claim in User.Claims)
            {
                if (claim.Type == ClaimTypes.NameIdentifier)
                {
                    githubId = claim.Value;
                }
                else if (claim.Type == ClaimTypes.Name)
                {
                    name = claim.Value;
                }
                else if (claim.Type == "urn:github:login")
                {
                    username = claim.Value;
                }
                else if (claim.Type == "urn:github:avatar")
                {
                    avatarUrl = claim.Value;
                }
            }

            ThisUser = await _context.GetUserByGithubId(githubId);

            int userId;

            if (ThisUser != null)
            {
                userId = ThisUser.Id;
            }
            else
            {
                ThisUser = new User {
                    Username   = username,
                    Name       = name,
                    GithubId   = githubId,
                    CreateDate = now,
                    AvatarUrl  = avatarUrl
                };

                var dbResponse = _context.AddUser(ThisUser);
                await _context.SaveChangesAsync();

                userId = dbResponse.Entity.Id;
            }

            Bookmarks = await _context.GetBookmarksByUserIdAsync(userId);

            FavoriteBookmarks = await _context.GetFavoriteBookmarksByUserIdAsync(userId);

            FollowingBookmarks = await _context.GetBookmarksByFollowersAsync(userId);
        }
示例#3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            DateTime now = DateTime.UtcNow;

            var bookmark = new Bookmark();

            // Grab information about the user. This will be used to associate user
            // information with the new bookmark.
            string githubId  = "";
            string name      = "";
            string username  = "";
            string avatarUrl = "";

            foreach (Claim claim in User.Claims)
            {
                if (claim.Type == ClaimTypes.NameIdentifier)
                {
                    githubId = claim.Value;
                }
                else if (claim.Type == ClaimTypes.Name)
                {
                    name = claim.Value;
                }
                else if (claim.Type == "urn:github:login")
                {
                    username = claim.Value;
                }
                else if (claim.Type == "urn:github:avatar")
                {
                    avatarUrl = claim.Value;
                }
            }

            // Using the bookmark URL, find the corresponding resource from the database.
            var existingResource = await _context.GetResourceByLocationAsync(BookmarkEntity.Location);

            // If it is found, take that existing resource and use its ID with the new bookmark.
            if (existingResource != null)
            {
                bookmark.ResourceId = existingResource.Id;
            }
            else
            {
                // If it isn't found, make a new resource.
                var metadata = new Dictionary <string, string>();

                string imageUrl = await ImageDownloaderHelper.FetchImageFromUrl(BookmarkEntity.Location);

                metadata.Add("ImageURL", imageUrl);

                var resource = new Resource
                {
                    Location   = BookmarkEntity.Location,
                    Metadata   = metadata,
                    CreateDate = now
                };

                // Write the resource to the database and use the ID that ends up being used.
                var dbResponse = _context.AddResource(resource);
                await _context.SaveChangesAsync();

                bookmark.ResourceId = dbResponse.Entity.Id;
            }

            // Repeat the process with adding a resource with adding a user.
            var existingUser = await _context.GetUserByGithubId(githubId);

            if (existingUser != null)
            {
                bookmark.UserId = existingUser.Id;
            }
            else
            {
                var user = new User
                {
                    Username   = username,
                    Name       = name,
                    GithubId   = githubId,
                    CreateDate = now,
                    AvatarUrl  = avatarUrl
                };

                existingUser = user;
                var dbResponse = _context.AddUser(user);
                await _context.SaveChangesAsync();

                bookmark.UserId = dbResponse.Entity.Id;
            }

            bookmark.CreateDate = now;

            bookmark.Title       = !string.IsNullOrWhiteSpace(BookmarkEntity.Title) ? BookmarkEntity.Title : null;
            bookmark.Description = !string.IsNullOrWhiteSpace(BookmarkEntity.Description) ? BookmarkEntity.Description : null;
            bookmark.Tags        = BookmarkTagHelper.StringToList(BookmarkEntity.Tags);
            bookmark.IsPublic    = BookmarkEntity.IsPublic;
            bookmark.Favorited   = BookmarkEntity.Favorited;

            _context.AddBookmark(bookmark);

            string userIdentifier = !string.IsNullOrWhiteSpace(existingUser.Name) ? existingUser.Name : existingUser.Username;

            Notification newNotification = new Notification {
                UserId     = existingUser.Id,
                CreateDate = DateTime.Now,
                Text       = $"{userIdentifier} added a new bookmark!"
            };

            _context.AddNotification(newNotification);

            await _context.SaveChangesAsync();

            string returnPage = Request.Query.ContainsKey("return") ? Request.Query["return"] : "/Bookmarks/Index";

            return(RedirectToPage(returnPage));
        }
示例#4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var existingUser = await _context.GetUserByUsernameAsync(UserEntity.Username);

            if (existingUser != null)
            {
                ModelState.AddModelError("Username", $"Username is already taken: {existingUser.Username}.");
            }

            // Add Roles
            var roles = new List <Role>();

            if (!string.IsNullOrWhiteSpace(UserEntity.Roles))
            {
                var rolenames = UserEntity.Roles.Split(',');
                foreach (var rolename in rolenames)
                {
                    if (string.IsNullOrWhiteSpace(rolename))
                    {
                        continue;
                    }

                    var r = await _context.GetRoleByNameAsync(rolename.Trim());

                    if (r == null)
                    {
                        ModelState.AddModelError("Roles", $"{rolename} does not exist.");
                        continue;
                    }

                    roles.Add(r);
                }
            }

            // Add Subscriptions
            var subscriptions = new List <User>();

            if (!string.IsNullOrWhiteSpace(UserEntity.Subscriptions))
            {
                var usernames = UserEntity.Subscriptions.Split(',');
                foreach (var username in usernames)
                {
                    if (string.IsNullOrWhiteSpace(username))
                    {
                        continue;
                    }

                    var account = await _context.GetUserByUsernameAsync(username.Trim());

                    if (account == null)
                    {
                        ModelState.AddModelError("Subscriptions", $"{username} does not exist.");
                        continue;
                    }

                    subscriptions.Add(account);
                }
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = new User
            {
                Username      = UserEntity.Username,
                Name          = UserEntity.Name,
                Bio           = UserEntity.Bio,
                AvatarUrl     = UserEntity.AvatarUrl,
                CreateDate    = DateTime.UtcNow,
                Roles         = roles,
                Subscriptions = subscriptions,
                Followers     = new List <User>()
            };

            _context.AddUser(user);
            await _context.SaveChangesAsync();

            TempData["Message"] = $"The user, {user.Username}, was successfully added.";

            _logger.LogWarning(AdminLoggingEvents.AddUserEvent, $"New user added: {user.Username}.");

            return(RedirectToPage("./Index"));
        }
示例#5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            List <ImportUserModel> importedUsers;

            try
            {
                importedUsers = JsonSerializer.Deserialize <List <ImportUserModel> >(Json);
            }
            catch
            {
                ModelState.AddModelError("Json", "Error reading JSON.");
                return(Page());
            }

            var entities = new List <Entities.User>();

            foreach (var user in importedUsers)
            {
                var entity = new Entities.User();
                entities.Add(entity);

                entity.Username   = user.Username;
                entity.Name       = user.Name;
                entity.AvatarUrl  = user.AvatarUrl;
                entity.Bio        = user.Bio;
                entity.CreateDate = DateTime.UtcNow;

                if (!string.IsNullOrWhiteSpace(user.Roles))
                {
                    var chosenRoles = user.Roles.Split(',');

                    foreach (var rolename in chosenRoles)
                    {
                        if (string.IsNullOrWhiteSpace(rolename))
                        {
                            continue;
                        }

                        var r = await _context.GetRoleByNameAsync(rolename.Trim());

                        if (r == null)
                        {
                            ModelState.AddModelError("Roles", $"{rolename} does not exist.");
                            continue;
                        }

                        entity.Roles.Add(r);
                    }
                }

                _context.AddUser(entity);
            }

            foreach (var user in importedUsers)
            {
                var entity = entities.Find(e => e.Username == user.Username);

                if (!string.IsNullOrWhiteSpace(user.Following))
                {
                    var chosenFollowers = user.Following.Split(',');
                    foreach (var username in chosenFollowers)
                    {
                        if (string.IsNullOrWhiteSpace(username))
                        {
                            continue;
                        }

                        var account = await _context.GetUserByUsernameAsync(username.Trim());

                        if (account == null)
                        {
                            var importedAccount = entities.FirstOrDefault(e => e.Username == username);
                            if (importedAccount == null)
                            {
                                ModelState.AddModelError("Following", $"{username} does not exist.");
                                continue;
                            }
                            else
                            {
                                entity.Subscriptions.Add(importedAccount);
                            }
                        }
                        else
                        {
                            entity.Subscriptions.Add(account);
                        }
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            try
            {
                await _context.SaveChangesAsync();

                TempData["Message"] = $"{importedUsers.Count} user(s) were successfully imported.";
                return(RedirectToPage("./Index"));
            }
            catch
            {
                ModelState.AddModelError("Json", "Error saving users to database.");
            }

            return(Page());
        }