public bool AddType(Type type)
 {
     if (type == null)
     {
         return(false);
     }
     type = type.ReduceType();
     if (MappedTypes.ContainsKey(type))
     {
         return(false);
     }
     MappedTypes[type] = ResolveType(type) ?? type;
     return(true);
 }
        public Type ResolveType(Type type)
        {
            type = type.ReduceType();
            Type result;

            if (MappedTypes.TryGetValue(type, out result))
            {
                return(result);
            }
            foreach (var resolverBase in EntitiesBuilder.TypeResolverList)
            {
                var resolved = resolverBase.Resolve(type);
                if (resolved != null)
                {
                    MappedTypes[type] = resolved;
                    return(resolved);
                }
                MappedTypes[type] = type;
            }
            return(null);
        }
        public void Load()
        {
            if (!File.Exists(CredentialManagerHelper.GetSaveFilePath()))
            {
                return;
            }

            using var stream = File.OpenRead(CredentialManagerHelper.GetSaveFilePath());
            using var reader = new BinaryReader(stream);
            var count = reader.ReadInt32();

            for (var i = 0; i < count; i++)
            {
                var type = reader.ReadString();
                if (MappedTypes.ContainsKey(type))
                {
                    var res = Activator.CreateInstance(MappedTypes[type]);

                    ((ICredentialEntry)res).Load(reader);
                    Credentials.Add(res as ICredentialEntry);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Handle the drag/drop event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_DragDrop(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(DataFormats.FileDrop) || ((string[])e.Data.GetData(DataFormats.FileDrop)).Length != 1)
            {
                return;
            }
            string FileName = "";

            try
            {
                FileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
                if (sender == lblDropMode)
                {
                    ProcessFile <int, String>(Modes, FileName, "ID_MODE(", ")", "= ", "", lblCountModes, "modes");
                }
                else if (sender == lblDropUserTypeFile)
                {
                    ProcessFile <int, String>(UserTypeNames, FileName, "USER(", ")", "= ", "", lblCountUserType, "usertypes");
                }
                else if (sender == lblDropUserName)
                {
                    ProcessFile <int, String>(UserNames, FileName, "USER(", ")", "= ", "", lblCountUserNames, "usernames");
                }
                else if (sender == lblDropArea)
                {
                    ProcessFile <int, String>(Areas, FileName, "ID_AREA(", ")", "= ", "", lblCountAreas, "areas");
                }
                else if (sender == lblDropPermission)
                {
                    ProcessFile <int[], bool>(Permissions, FileName, "_AREA_MODE(", ")", "= ", "", lblCountPermissions, "permissions");
                }
                else if (sender == lblDropOldOperatorships)
                {
                    ProcessFile(OldPermissions, FileName, lblCountAreas, "operatorships");
                }
                else if (sender == lblDropUserTypeToModeLinkages)
                {
                    String     InLine;
                    List <int> MappedTypes = null;
                    UserTypeToModeLinkages.Clear();
                    using (StreamReader sRd = new StreamReader(FileName))
                        while ((InLine = sRd.ReadLine()) != null)
                        {
                            if (InLine.StartsWith("USRTYP,"))
                            {
                                int StartStr = InLine.IndexOf("ID='") + 4;
                                UserTypeToModeLinkages.Add(InLine.Substring(StartStr, InLine.IndexOf("'", StartStr + 1) - StartStr), MappedTypes = new List <int>());
                            }
                            else if (InLine.StartsWith("UMODE,"))
                            {
                                MappedTypes.Add(int.Parse(InLine.Substring(InLine.LastIndexOf('=') + 1)));
                            }
                        }

                    foreach (List <int> MappedType in UserTypeToModeLinkages.Values)
                    {
                        MappedType.TrimExcess();
                    }
                    lblCountUserTypeToModeLinkages.Text = UserTypeToModeLinkages.Count.ToString() + " user->mode";
                }

                //Now, if we have enough data, produce our list
                if (Permissions.Count > 0 && Areas.Count > 0 && UserNames.Count > 0 && UserTypeNames.Count > 0 && Modes.Count > 0 && UserTypeToModeLinkages.Count > 0)
                {
                    NewPermissions.Clear();

                    //Now, perform our merging
                    List <String> MissingUsers = new List <String>();
                    List <int>    MissingAreas = new List <int>();
                    Dictionary <String, String> UserNameToUserType = new Dictionary <string, string>();

                    List <int> UserTypeToModeLinkage;
                    foreach (KeyValuePair <int, string> User in UserNames)
                    {
                        if (!UserTypeToModeLinkages.TryGetValue(UserTypeNames[User.Key], out UserTypeToModeLinkage))
                        {
                            MissingUsers.Add(UserTypeNames[User.Key]);
                        }
                        else
                        {
                            UserNameToUserType.Add(User.Value, UserTypeNames[User.Key]);
                            List <String> OutAreas = new List <string>();
                            String        FoundArea;
                            foreach (KeyValuePair <int[], bool> kvp in Permissions)
                            {
                                if (kvp.Value && UserTypeToModeLinkage.Contains(kvp.Key[1]))
                                {
                                    if (!Areas.TryGetValue(kvp.Key[0], out FoundArea))
                                    {
                                        MissingAreas.Add(kvp.Key[0]);
                                    }
                                    else if (!OutAreas.Contains(FoundArea))
                                    {
                                        OutAreas.Add(FoundArea);
                                    }
                                }
                            }
                            OutAreas.Sort(StringComparer.CurrentCultureIgnoreCase);
                            if (OutAreas.Count > 0)
                            {
                                NewPermissions.Add(User.Value, OutAreas.ToArray());
                            }
                        }
                    }


                    //Now, clear our ListView
                    lvOutputs.Items.Clear();
                    if (OldPermissions.Count > 0)
                    {
                        String[] FoundPermissions;
                        foreach (KeyValuePair <String, String[]> kvp in NewPermissions)
                        {
                            if (!OldPermissions.TryGetValue(kvp.Key, out FoundPermissions))
                            {
                                ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                                lvI.SubItems.Add(UserNameToUserType[kvp.Key]);
                                lvI.SubItems.Add("Yes");
                                lvI.SubItems.Add(String.Join(",", kvp.Value));
                                lvI.SubItems.Add("");
                            }
                            else
                            {
                                List <String> Additions = new List <string>();
                                List <String> Removals  = new List <string>();
                                foreach (String str in FoundPermissions)
                                {
                                    if (Array.IndexOf(kvp.Value, str) == -1)
                                    {
                                        Removals.Add(str);
                                    }
                                }
                                foreach (String str in kvp.Value)
                                {
                                    if (Array.IndexOf(FoundPermissions, str) == -1)
                                    {
                                        Additions.Add(str);
                                    }
                                }
                                if (Additions.Count + Removals.Count > 0)
                                {
                                    ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                                    lvI.SubItems.Add(UserNameToUserType[kvp.Key]);
                                    lvI.SubItems.Add("");
                                    lvI.SubItems.Add(String.Join(",", Additions.ToArray()));
                                    lvI.SubItems.Add(String.Join(",", Removals.ToArray()));
                                }
                            }
                        }

                        //Now, pull in our old ones that are missing
                        foreach (KeyValuePair <String, String[]> kvp in OldPermissions)
                        {
                            if (!NewPermissions.ContainsKey(kvp.Key))
                            {
                                ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                                lvI.SubItems.Add("Old");
                                lvI.SubItems.Add(String.Join(",", kvp.Value.ToArray()));
                                lvI.SubItems.Add("");
                            }
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair <String, String[]> kvp in NewPermissions)
                        {
                            ListViewItem lvI = lvOutputs.Items.Add(kvp.Key);
                            lvI.SubItems.Add(UserNameToUserType[kvp.Key]);

                            lvI.SubItems.Add("Yes");
                            lvI.SubItems.Add(String.Join(",", kvp.Value));
                            lvI.SubItems.Add("");
                        }
                    }

                    lvOutputs.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                    lvOutputs.Sort();
                    if (MissingAreas.Count > 0)
                    {
                        MessageBox.Show("Missing " + MissingAreas.Count.ToString() + " areas: " + String.Join(", ", MissingAreas.ToArray()));
                    }


                    if (MissingUsers.Count > 0)
                    {
                        MessageBox.Show("Missing " + MissingUsers.Count.ToString() + " users: " + String.Join(", ", MissingUsers.ToArray()));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading file " + FileName + ":" + ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }