예제 #1
0
        public static void Initialize(string clientId, string applicationBundle = null)
        {
            ClientId = clientId;

            if (applicationBundle != null)
            {
                RedirectURI = RedirectURI.Replace("zbl.oauth2", applicationBundle);
            }

            UIRuntime.OnActivated.Handle(OnNavigatingTo);
        }
        public async Task <IActionResult> Edit(string id, [Bind("ClientDescription", "RedirectUris")] EditClientViewModel vm)
        {
            string      uid    = _userManager.GetUserId(this.User);
            OAuthClient client = await _context.ClientApplications.Include(x => x.Owner).Include(x => x.RedirectURIs).Where(x => x.ClientId == id && x.Owner.Id == uid).FirstOrDefaultAsync();

            if (client == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try {
                    List <RedirectURI> originalUris = client.RedirectURIs;
                    CheckAndMark(originalUris, vm.RedirectUris);

                    client.ClientDescription = vm.ClientDescription;
                    _context.Update(client);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException) {
                    if (!OAuthClientExists(vm.ClientId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(vm));


            void CheckAndMark(List <RedirectURI> originals, IEnumerable <string> submitted)
            {
                List <RedirectURI> newList = new List <RedirectURI>();

                foreach (string s in submitted)
                {
                    if (String.IsNullOrWhiteSpace(s))
                    {
                        continue;
                    }
                    RedirectURI fromOld = originals.FirstOrDefault(x => x.URI == s);
                    if (fromOld == null)
                    {
                        // this 's' is new.
                        RedirectURI rdi = new RedirectURI()
                        {
                            OAuthClient = client, OAuthClientId = client.ClientId, URI = s
                        };
                        newList.Add(rdi);
                    }
                    else
                    {
                        // this 's' was re-submitted
                        newList.Add(fromOld);
                    }
                }

                // Marking deleted Redirect URIs for Deletion.
                originals.Except(newList).Select(x => _context.Entry(x).State = EntityState.Deleted);

                // Assign the new list back to the client
                client.RedirectURIs = newList;
            }
        }