private void FillVisibilityListBox()
 {
     PluginHandler.MainForm.DoThreadSafe(new Action(() =>
     {
         try
         {
             //((ListBox)clbVisibilityToUsers).DataSource = null;
             if (MyVisibility != null)
             {
                 ((ListBox)clbVisibilityToUsers).DataSource = MyVisibility.UserRightsObservable;
             }
             ((ListBox)clbVisibilityToUsers).DisplayMember = "EMail";
             ((ListBox)clbVisibilityToUsers).ValueMember   = "CanEdit";
             for (int i = 0; i < clbVisibilityToUsers.Items.Count; i++)
             {
                 SINerUserRight obj = (SINerUserRight)clbVisibilityToUsers.Items[i];
                 clbVisibilityToUsers.SetItemChecked(i, obj.CanEdit != null && obj.CanEdit.Value);
             }
             clbVisibilityToUsers.Refresh();
         }
         catch (Exception e)
         {
             System.Diagnostics.Trace.TraceError(e.Message, e);
             Console.WriteLine(e);
             throw;
         }
     }));
 }
Beispiel #2
0
        private void FillVisibilityListBox()
        {
            PluginHandler.MainForm.DoThreadSafe(new Action(() =>
            {
                try
                {
                    this.cbVisibilityIsPublic.CheckedChanged       -= cbVisibilityIsPublic_CheckedChanged;
                    this.cbVisibilityIsGroupVisible.CheckedChanged -= cbVisibilityIsGroupVisible_CheckedChanged;
                    SINnersOptions.SINnerVisibility            = null;
                    ((ListBox)clbVisibilityToUsers).DataSource = null;
                    if (SINnersOptions.SINnerVisibility != null)
                    {
                        if (SINnersOptions.SINnerVisibility.IsPublic != null)
                        {
                            this.cbVisibilityIsPublic.Checked = SINnersOptions.SINnerVisibility.IsPublic.Value;
                        }
                        if (SINnersOptions.SINnerVisibility.IsGroupVisible != null)
                        {
                            this.cbVisibilityIsGroupVisible.Checked = SINnersOptions.SINnerVisibility.IsGroupVisible.Value;
                        }
                        ((ListBox)clbVisibilityToUsers).DataSource = SINnersOptions.SINnerVisibility.UserRightsObservable;
                    }

                    ((ListBox)clbVisibilityToUsers).DisplayMember = "EMail";
                    ((ListBox)clbVisibilityToUsers).ValueMember   = "CanEdit";
                    for (int i = 0; i < clbVisibilityToUsers.Items.Count; i++)
                    {
                        SINerUserRight obj = (SINerUserRight)clbVisibilityToUsers.Items[i];
                        clbVisibilityToUsers.SetItemChecked(i, obj.CanEdit != null && obj.CanEdit.Value);
                    }
                    clbVisibilityToUsers.Refresh();
                    this.cbVisibilityIsPublic.CheckedChanged       += cbVisibilityIsPublic_CheckedChanged;
                    this.cbVisibilityIsGroupVisible.CheckedChanged += cbVisibilityIsGroupVisible_CheckedChanged;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.TraceError(e.Message, e);
                    Console.WriteLine(e);
                    throw;
                }
            }));
        }
Beispiel #3
0
        private void InitializeMe()
        {
            cbSINnerUrl.SelectedValueChanged -= CbSINnerUrl_SelectedValueChanged;
            Properties.Settings.Default.Reload();
            var sinnerurl = Properties.Settings.Default.SINnerUrl;

            this.cbSINnerUrl.DataSource              = Properties.Settings.Default.SINnerUrls;
            this.cbSINnerUrl.SelectedItem            = sinnerurl;
            this.cbVisibilityIsPublic.BindingContext = new BindingContext();
            var t = StartSTATask(
                async() =>
            {
                var roles = await GetRolesStatus();
                UpdateDisplay();
                if (!roles.Any())
                {
                    ShowWebBrowser();
                }
            });

            //var t = Task.Run(
            //    async () =>
            //    {
            //        await GetRolesStatus();
            //        UpdateDisplay();
            //    });
            this.cbVisibilityIsPublic.Checked             = SINnersOptions.SINnerVisibility.IsPublic.Value;
            this.cbVisibilityIsGroupVisible.Checked       = SINnersOptions.SINnerVisibility.IsGroupVisible.Value;
            ((ListBox)clbVisibilityToUsers).DataSource    = SINnersOptions.SINnerVisibility.UserRightsObservable;
            ((ListBox)clbVisibilityToUsers).DisplayMember = "EMail";
            ((ListBox)clbVisibilityToUsers).ValueMember   = "CanEdit";
            for (int i = 0; i < clbVisibilityToUsers.Items.Count; i++)
            {
                SINerUserRight obj = (SINerUserRight)clbVisibilityToUsers.Items[i];
                clbVisibilityToUsers.SetItemChecked(i, obj.CanEdit.Value);
            }
            cbUploadOnSave.Checked            = SINnersOptions.UploadOnSave;
            cbSINnerUrl.SelectedValueChanged += CbSINnerUrl_SelectedValueChanged;
        }
Beispiel #4
0
        public static void AddVisibilityForEmail(string email)
        {
            if (!IsValidEmail(email))
            {
                MessageBox.Show("Please enter a valid email address!");
                return;
            }
            SINerUserRight ur = new SINerUserRight()
            {
                EMail   = email,
                CanEdit = true,
                Id      = Guid.NewGuid()
            };
            var found = from a in SINnersOptions.SINnerVisibility.UserRightsObservable where a.EMail.ToLowerInvariant() == email.ToLowerInvariant() select a;

            if (found.Any())
            {
                ur = found.FirstOrDefault();
            }
            if (!SINnersOptions.SINnerVisibility.UserRightsObservable.Contains(ur))
            {
                SINnersOptions.SINnerVisibility.UserRightsObservable.Add(ur);
            }
        }
Beispiel #5
0
        private async Task <IActionResult> PostSINnerInternal(UploadInfoObject uploadInfo)
        {
            _logger.LogTrace("Post SINnerInternalt: " + uploadInfo + ".");
            ApplicationUser user   = null;
            SINner          sinner = null;

            try
            {
                if (!ModelState.IsValid)
                {
                    var errors = ModelState.Select(x => x.Value.Errors)
                                 .Where(y => y.Count > 0)
                                 .ToList();
                    string msg = "ModelState is invalid: ";
                    foreach (var err in errors)
                    {
                        foreach (var singleerr in err)
                        {
                            msg += Environment.NewLine + "\t" + singleerr.ToString();
                        }
                    }
                    return(BadRequest(new HubException(msg)));
                }
                if (uploadInfo.UploadDateTime == null)
                {
                    uploadInfo.UploadDateTime = DateTime.Now;
                }
                if (uploadInfo.Client != null)
                {
                    if (!UploadClientExists(uploadInfo.Client.Id))
                    {
                        _context.UploadClients.Add(uploadInfo.Client);
                    }
                    else
                    {
                        _context.UploadClients.Attach(uploadInfo.Client);
                        _context.Entry(uploadInfo.Client).State = EntityState.Modified;
                        _context.Entry(uploadInfo.Client).CurrentValues.SetValues(uploadInfo.Client);
                    }
                }
                var returncode = HttpStatusCode.OK;
                user = await _signInManager.UserManager.FindByNameAsync(User.Identity.Name);

                foreach (var tempsinner in uploadInfo.SINners)
                {
                    sinner = tempsinner;
                    if (sinner.Id.ToString() == "string")
                    {
                        sinner.Id = Guid.Empty;
                    }

                    if (String.IsNullOrEmpty(sinner.MyExtendedAttributes.JsonSummary))
                    {
                        return(BadRequest("sinner " + sinner.Id + ": JsonSummary == null"));
                    }

                    if (sinner.SINnerMetaData.Visibility.UserRights.Any() == false)
                    {
                        return(BadRequest("Sinner  " + sinner.Id + ": Visibility contains no entries!"));
                    }

                    if (sinner.LastChange == null)
                    {
                        return(BadRequest("Sinner  " + sinner.Id + ": LastChange not set!"));
                    }
                    if ((sinner.SINnerMetaData.Visibility.Id == null) ||
                        (sinner.SINnerMetaData.Visibility.Id == Guid.Empty))
                    {
                        sinner.SINnerMetaData.Visibility.Id = Guid.NewGuid();
                    }
                    var oldsinner = (from a in _context.SINners.Include(a => a.SINnerMetaData.Visibility.UserRights)
                                     .Include(b => b.MyGroup)
                                     where a.Id == sinner.Id
                                     select a).FirstOrDefault();
                    if (oldsinner != null)
                    {
                        var  olduserrights = oldsinner.SINnerMetaData.Visibility.UserRights.ToList();
                        bool canedit       = false;
                        foreach (var oldright in olduserrights)
                        {
                            if ((oldright.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant() &&
                                 (oldright.CanEdit == true)))
                            {
                                canedit = true;
                                break;
                            }
                        }
                        if (!canedit)
                        {
                            string msg = "SINner " + sinner.Id + " is not editable for user " + user.Email + ".";
                            throw new NoUserRightException(msg);
                        }
                        _context.UserRights.RemoveRange(olduserrights);
                        bool userfound = false;
                        foreach (var ur in sinner.SINnerMetaData.Visibility.UserRights)
                        {
                            if (ur.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant())
                            {
                                ur.CanEdit = true;
                                userfound  = true;
                            }
                            ur.Id       = Guid.NewGuid();
                            ur.SINnerId = sinner.Id;
                            _context.UserRights.Add(ur);
                        }
                        if (!userfound)
                        {
                            SINerUserRight ownUser = new SINerUserRight();
                            ownUser.Id       = Guid.NewGuid();
                            ownUser.SINnerId = sinner.Id;
                            ownUser.CanEdit  = true;
                            ownUser.EMail    = user.Email;
                            sinner.SINnerMetaData.Visibility.UserRights.Add(ownUser);
                            _context.UserRights.Add(ownUser);
                        }
                    }
                    else
                    {
                        var ownuserfound = false;
                        var list         = sinner.SINnerMetaData.Visibility.UserRights.ToList();
                        foreach (var ur in list)
                        {
                            ur.SINnerId = sinner.Id;
                            if (ur.EMail.ToLowerInvariant() == "*****@*****.**".ToLowerInvariant())
                            {
                                sinner.SINnerMetaData.Visibility.UserRights.Remove(ur);
                            }
                            if (ur.EMail.ToLowerInvariant() == user.Email.ToLowerInvariant())
                            {
                                ownuserfound = true;
                            }
                        }
                        if (!ownuserfound)
                        {
                            SINerUserRight ownright = new SINerUserRight();
                            ownright.CanEdit  = true;
                            ownright.EMail    = user.Email;
                            ownright.SINnerId = sinner.Id;
                            ownright.Id       = Guid.NewGuid();
                            sinner.SINnerMetaData.Visibility.UserRights.Add(ownright);
                        }
                    }

                    foreach (var tag in sinner.SINnerMetaData.Tags)
                    {
                        tag.SetSinnerIdRecursive(sinner.Id);
                    }

                    sinner.UploadClientId = uploadInfo.Client.Id;
                    SINner dbsinner = await CheckIfUpdateSINnerFile(sinner.Id.Value, user);

                    SINnerGroup oldgroup = null;
                    if (dbsinner != null)
                    {
                        oldgroup = dbsinner.MyGroup;
                        _context.SINners.Attach(dbsinner);
                        if (String.IsNullOrEmpty(sinner.GoogleDriveFileId))
                        {
                            sinner.GoogleDriveFileId = dbsinner.GoogleDriveFileId;
                        }
                        if (String.IsNullOrEmpty(sinner.DownloadUrl))
                        {
                            sinner.DownloadUrl = dbsinner.DownloadUrl;
                        }

                        _context.UserRights.RemoveRange(dbsinner.SINnerMetaData.Visibility.UserRights);
                        _context.SINnerVisibility.Remove(dbsinner.SINnerMetaData.Visibility);
                        var alltags = await dbsinner.GetTagsForSinnerFlat(_context);

                        _context.Tags.RemoveRange(alltags);
                        _context.SINnerMetaData.Remove(dbsinner.SINnerMetaData);
                        _context.SINners.Remove(dbsinner);
                        dbsinner.SINnerMetaData.Visibility.UserRights.Clear();
                        dbsinner.SINnerMetaData.Visibility = null;
                        dbsinner.SINnerMetaData.Tags       = null;
                        dbsinner.SINnerMetaData            = null;

                        await _context.SaveChangesAsync();

                        await _context.SINners.AddAsync(sinner);

                        string msg = "Sinner " + sinner.Id + " updated: " + _context.Entry(dbsinner).State.ToString();
                        msg += Environment.NewLine + Environment.NewLine + "LastChange: " + dbsinner.LastChange;
                        _logger.LogError(msg);
                        List <Tag> taglist = sinner.SINnerMetaData.Tags;
                        UpdateEntityEntries(taglist);
                    }
                    else
                    {
                        returncode     = HttpStatusCode.Created;
                        sinner.MyGroup = null;
                        _context.SINners.Add(sinner);
                    }

                    try
                    {
                        await _context.SaveChangesAsync();

                        if (oldgroup != null)
                        {
                            var roles = await _userManager.GetRolesAsync(user);

                            await SINnerGroupController.PutSiNerInGroupInternal(oldgroup.Id.Value, sinner.Id.Value, user, _context,
                                                                                _logger, oldgroup.PasswordHash, roles);
                        }
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        foreach (var entry in ex.Entries)
                        {
                            if (entry.Entity is SINner)
                            {
                                Utils.DbUpdateConcurrencyExceptionHandler(entry, _logger);
                            }
                            else if (entry.Entity is Tag)
                            {
                                Utils.DbUpdateConcurrencyExceptionHandler(entry, _logger);
                            }
                            else
                            {
                                throw new NotSupportedException(
                                          "Don't know how to handle concurrency conflicts for "
                                          + entry.Metadata.Name);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            var tc = new Microsoft.ApplicationInsights.TelemetryClient();
                            Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e);
                            telemetry.Properties.Add("User", user?.Email);
                            telemetry.Properties.Add("SINnerId", sinner?.Id?.ToString());
                            tc.TrackException(telemetry);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex.ToString());
                        }
                        if (e is HubException)
                        {
                            return(Conflict(e));
                        }

                        HubException hue = new HubException("Exception in PostSINnerFile: " + e.ToString(), e);
                        //var msg = new System.Net.Http.HttpResponseMessage(HttpStatusCode.Conflict) { ReasonPhrase = e.Message };
                        return(Conflict(hue));
                    }
                }

                List <Guid> myids = (from a in uploadInfo.SINners select a.Id.Value).ToList();
                switch (returncode)
                {
                case HttpStatusCode.OK:
                    return(Accepted("PostSIN", myids));

                case HttpStatusCode.Created:
                    return(CreatedAtAction("PostSIN", myids));

                default:
                    break;
                }
                return(BadRequest());
            }
            catch (Exception e)
            {
                try
                {
                    var tc = new Microsoft.ApplicationInsights.TelemetryClient();
                    Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry telemetry = new Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry(e);
                    telemetry.Properties.Add("User", user?.Email);
                    telemetry.Properties.Add("SINnerId", sinner?.Id?.ToString());
                    tc.TrackException(telemetry);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString());
                }
                if (e is HubException)
                {
                    return(BadRequest(e));
                }

                HubException hue = new HubException("Exception in PostSINnerFile: " + e.Message, e);
                return(BadRequest(hue));
            }
        }
Beispiel #6
0
 public async void UpdateDisplay()
 {
     PluginHandler.MainForm.DoThreadSafe(new Action(() =>
     {
         try
         {
             if (LoginStatus == true)
             {
                 var t = GetUserEmail();
                 t.ContinueWith((emailtask) =>
                 {
                     string mail = emailtask.Result;
                     if (!String.IsNullOrEmpty(mail))
                     {
                         Properties.Settings.Default.UserEmail = mail;
                         bool createVis = false;
                         if (String.IsNullOrEmpty(Properties.Settings.Default.SINnerVisibility))
                         {
                             createVis = true;
                         }
                         else
                         {
                             SINnerVisibility vis = Newtonsoft.Json.JsonConvert.DeserializeObject <SINnerVisibility>(Properties.Settings.Default.SINnerVisibility);
                             bool found           = false;
                             foreach (var ur in vis.UserRights)
                             {
                                 if (ur.EMail.ToLowerInvariant() == mail.ToLowerInvariant())
                                 {
                                     ur.CanEdit = true;
                                     found      = true;
                                     break;
                                 }
                             }
                             if (!found)
                             {
                                 createVis = true;
                             }
                         }
                         if (createVis)
                         {
                             SINnerVisibility vis = new SINnerVisibility();
                             vis.Id             = Guid.NewGuid();
                             vis.IsGroupVisible = true;
                             vis.IsPublic       = true;
                             vis.UserRights     = new List <SINerUserRight>();
                             SINerUserRight ur  = new SINerUserRight();
                             ur.Id      = Guid.NewGuid();
                             ur.EMail   = mail;
                             ur.CanEdit = true;
                             vis.UserRights.Add(ur);
                             SINnersOptions.SINnerVisibility = null;
                             SINnersOptions.SINnerVisibility = vis;
                             Properties.Settings.Default.SINnerVisibility = Newtonsoft.Json.JsonConvert.SerializeObject(vis);
                             Properties.Settings.Default.Save();
                             FillVisibilityListBox();
                         }
                         //also, since we are logged in in now, refresh the frmCharacterRoster!
                         PluginHandler.MainForm.DoThreadSafe(() =>
                         {
                             PluginHandler.MainForm.CharacterRoster.LoadCharacters(true, true, true, true);
                         });
                     }
                 });
                 this.bLogin.Text             = "Logout";
                 string status                = StaticUtils.UserRoles.Aggregate((a, b) => a + ", " + b);
                 labelAccountStatus.Text      = status;
                 labelAccountStatus.ForeColor = Color.DarkGreen;
                 HideWebBrowser();
             }
             else if (LoginStatus == false)
             {
                 this.bLogin.Text                  = "Login";
                 this.labelAccountStatus.Text      = "logged out";
                 this.labelAccountStatus.ForeColor = Color.DarkRed;
             }
             else
             {
                 this.bLogin.Text                  = "Login";
                 this.labelAccountStatus.Text      = "unknown";
                 this.labelAccountStatus.ForeColor = Color.DeepPink;
             }
         }
         catch (Exception ex)
         {
             System.Diagnostics.Trace.TraceError(ex.ToString());
         }
     }));
 }
Beispiel #7
0
 public async void UpdateDisplay()
 {
     PluginHandler.MainForm.DoThreadSafe(new Action(() =>
     {
         try
         {
             if (LoginStatus == true)
             {
                 if (String.IsNullOrEmpty(Properties.Settings.Default.UserEmail))
                 {
                     var t = GetUserEmail();
                     t.ContinueWith((emailtask) =>
                     {
                         string mail = emailtask.Result;
                         if (!String.IsNullOrEmpty(mail))
                         {
                             Properties.Settings.Default.UserEmail = mail;
                             if (String.IsNullOrEmpty(Properties.Settings.Default.SINnerVisibility))
                             {
                                 SINnerVisibility vis = new SINnerVisibility();
                                 vis.Id             = Guid.NewGuid();
                                 vis.Groupname      = "ChangeThis!";
                                 vis.IsGroupVisible = true;
                                 vis.IsPublic       = true;
                                 vis.UserRights     = new List <SINerUserRight>();
                                 SINerUserRight ur  = new SINerUserRight();
                                 ur.Id      = Guid.NewGuid();
                                 ur.EMail   = mail;
                                 ur.CanEdit = true;
                                 vis.UserRights.Add(ur);
                                 Properties.Settings.Default.SINnerVisibility = Newtonsoft.Json.JsonConvert.SerializeObject(vis);
                                 Properties.Settings.Default.Save();
                             }
                         }
                     });
                 }
                 this.bLogin.Text             = "Logout";
                 string status                = Roles.Aggregate((a, b) => a + ", " + b);
                 labelAccountStatus.Text      = status;
                 labelAccountStatus.ForeColor = Color.DarkGreen;
                 HideWebBrowser();
             }
             else if (LoginStatus == false)
             {
                 this.bLogin.Text                  = "Login";
                 this.labelAccountStatus.Text      = "logged out";
                 this.labelAccountStatus.ForeColor = Color.DarkRed;
             }
             else
             {
                 this.bLogin.Text                  = "Login";
                 this.labelAccountStatus.Text      = "unknown";
                 this.labelAccountStatus.ForeColor = Color.DeepPink;
             }
         }
         catch (Exception ex)
         {
             System.Diagnostics.Trace.TraceError(ex.ToString());
         }
     }));
 }