/// <summary>
        /// The function creates a new PwEntry that represents an import source.
        /// </summary>
        /// <param name="fileName">The path to the file in your filesystem that should be used
        /// as import source.</param>
        /// <returns><c>ChangeFlags.CommonChange</c> if the function has made any changes to the
        /// actual database structure (means if we added he impSrc). 0 if the impSrc allready exists
        /// and we don't have to make any changes anymore.</returns>
        public Changes AddImportPath(string fileName)
        {
            //is it a valid path to a file?
            if (!File.Exists(fileName))
            {
                return(Changes.None);
            }
            //check if allready exists
            PwGroup importGroup = m_database.GetImportGroup();

            foreach (PwEntry src in importGroup.GetEntries(true))
            {
                if (src.Strings.ReadSafe(KeeShare.SourcePathField) == fileName)
                {
                    return(Changes.None);
                }
            }

            PwGroup newImportGroup = new PwGroup(true, true, fileName, PwIcon.NetworkServer);

            newImportGroup.SetParent(importGroup);
            PwEntry newImportSource = new PwEntry(true, true);

            newImportSource.Strings.Set(KeeShare.TitleField, new ProtectedString(false, fileName));
            newImportSource.Strings.Set(KeeShare.SourcePathField, new ProtectedString(false, fileName));
            newImportSource.SetParent(newImportGroup);
            return(Changes.GroupCreated);
        }
Example #2
0
        /// <summary>
        /// Propagates possible changes of a rootNode to all of his proxyNodes
        /// </summary>
        /// <param name="root">The rootNode we want to propagate</param>
        /// <returns>True if the function has made changes to the database.</returns>
        private Changes UpdateProxyInformation(PwEntry root)
        {
            Changes changeFlag = Changes.None;
            //get all relevant proxies
            //copy new information from root to proxies
            PwObjectList <PwEntry> allProxies = m_database.GetAllProxyNodes();

            foreach (PwEntry proxy in allProxies)
            {
                //check if the proxy matches to the root and has changes! if not we are done here
                if (proxy.IsProxyOf(root) && !proxy.IsSimilarTo(root, true))
                {
                    PwGroup parent  = proxy.ParentGroup;
                    bool    success = parent.Entries.Remove(proxy);
                    Debug.Assert(success);
                    PwEntry duplicate = root.CloneDeep();
                    duplicate.Uuid = proxy.Uuid;
                    //if the rootNode was a userRoot, the StringFiledUidLink is set automatically in a clone
                    //but if not we have to set it manually
                    if (!root.IsUserRootNode())
                    {
                        duplicate.MakeToProxyOf(root);
                    }
                    duplicate.SetParent(parent);
                    changeFlag |= Changes.EntryCreated;
                }
            }
            return(changeFlag);
        }
Example #3
0
        public static PwEntry DuplicateTo(this PwEntry entry, PwGroup parent)
        {
            PwEntry copy = new PwEntry(false, false);

            copy.Uuid = entry.Uuid;
            copy.AssignProperties(entry, false, false, true);
            //TODO MST: clear this comment and it's ramifications!
            //HACK: CloneDeep introduces the copy into the parent node, therefore SetParent triggers a change of the parent
            //      which shouldn't be - the extended ProtectionSection in KeeShare should prevent interference, but a
            //      a clean way to clone a node without cloning children and without hooking it into a tree would be nice
            copy.SetParent(parent);
            return(copy);
        }
Example #4
0
        /// <summary>
        /// The function checks if thelast made change has to be propageted to
        /// some referenced PwEntries
        /// </summary>
        /// <returns>True if the function has made changes to the database.</returns>
        private Changes CheckReferences()
        {
            PwEntry lastModifiedEntry = GetLastModifiedEntry();

            //if there are no changes, then we have nothing to do
            if (lastModifiedEntry == null)
            {
                return(Changes.None);
            }
            //was it a proxy or not?
            Changes changeFlag = Changes.None;

            if (lastModifiedEntry.IsProxyNode())
            {
                //lets update the root so we later can update all proxies
                PwEntry root = m_database.GetProxyTargetFor(lastModifiedEntry);
                //check if there are real changes! if not we are done here
                if (lastModifiedEntry.IsSimilarTo(root, true))
                {
                    return(Changes.None);
                }
                PwGroup parent = root.ParentGroup;

                root.CreateBackup(m_database); //rootNode_X should save all modifications in history
                parent.Entries.Remove(root);

                PwEntry updatedRoot = lastModifiedEntry.CloneDeep();
                updatedRoot.Uuid = root.Uuid;
                updatedRoot.SetParent(parent);
                //special handling for userRootNodes because they have a homefolder
                if (root.IsUserRootNode())
                {
                    //maybe the oldUserName has changed to => the homefolder should have the new name also
                    //we also want to have the same icons everywhere
                    parent.Name   = updatedRoot.GetTitle();
                    parent.IconId = updatedRoot.IconId;
                }
                else
                {
                    updatedRoot.Strings.Remove(KeeShare.UuidLinkField);
                }
                changeFlag |= UpdateProxyInformation(updatedRoot);
                changeFlag |= Changes.GroupDeleted;
            }
            else
            {
                changeFlag |= UpdateProxyInformation(lastModifiedEntry);
            }
            pe_lastModedEntry = GetLastModifiedEntry();
            return(changeFlag);
        }
Example #5
0
        private Changes RestoreMovedUserNode(PwEntry userNode, PwGroup userHome, PwGroup usersGroup)
        {
            //only the userNode was moved..
            //are we located anywhere in the "users"group => here we have to be careful!
            if (userNode.IsInsideParent(usersGroup))
            {
                //do we have a applicable homeFolder?
                if (null != userHome)
                {
                    //then we should only move back!
                    userNode.ParentGroup.Entries.Remove(userNode);
                    userNode.SetParent(userHome);
                    //and we are done here...
                    return(Changes.EntryMoved);
                }
            }
            else
            {
                //else userNode was moved intentionally to share a folder, so move the rootNode back
                //and create a proxy at the new location
                PwGroup parentGroup = userNode.ParentGroup;
                parentGroup.Entries.Remove(userNode);
                //ensure that the original rootNode is located in his homefolder
                //move the original userRootNode back to his homefolder
                userNode.SetParent(m_database.GetUserHomeFor(userNode));

                //create proxyNode in the folder
                if (!GroupIsSharedToUser(parentGroup, userNode))
                {
                    PwEntry proxy = userNode.CreateProxyNode();
                    proxy.SetParent(parentGroup);
                    return(Changes.EntryMoved | Changes.EntryCreated);
                }
                return(Changes.EntryMoved);
            }
            return(Changes.None);
        }
Example #6
0
        /// <summary>
        /// The <c>ShareGroupWithUser</c> function adds an user-proxy to the group group.
        /// This userentry specifies the user how should become access to the group group.
        /// </summary>
        /// <param name="group">The group we want to share.</param>
        /// <param name="root">userRootNode of the user we want to share the group with.</param>
        private void ShareGroupWithUser(PwGroup group, PwEntry root)
        {
            //if (!UserExists(name)) return;
            //check if group is shared allready
            if (GroupIsSharedToUser(group, root))
            {
                return;
            }
            PwEntry proxyNode = root.CreateProxyNode();

            if (proxyNode != null)
            {
                proxyNode.SetParent(group);
            }
        }
Example #7
0
        /// <summary>
        /// The <c>CreateNewUser</c> function creates a new user you
        /// later can share passwords with.
        /// This includes a proxy in the UsersGroupName group and also
        /// creates a new user-specific group in the tree UsersGroupName
        /// </summary>
        /// <param name="name">oldUserName of the new user</param>
        /// <returns>True if all is done properly! False otherwise!</returns>
        protected Changes CreateNewUser(string name, PwGroup useAsHome = null)
        {
            PwEntry newUser      = CreateUserNode(name);
            PwGroup newUserGroup = useAsHome;

            if (newUserGroup == null) //create a new home for that user
            {
                newUserGroup = m_database.GetUserHomeFor(newUser, true);
            }
            else
            {
                newUserGroup.IconId = PwIcon.UserKey;
                newUserGroup.Notes += newUser.Uuid.ToHexString();
            }

            //put the userRootNode into his homefolder
            newUser.SetParent(newUserGroup);
            //due to better userHandling while dragNdrop we create a proxyNode in the usersGroup
            PwEntry proxy = newUser.CreateProxyNode();

            proxy.SetParent(m_database.GetUsersGroup());
            return(Changes.GroupCreated | Changes.EntryCreated);
        }
Example #8
0
        private Changes EnsureUsersProxiesInUsersGroup()
        {
            PwGroup usersGroup = m_database.GetUsersGroup();
            PwObjectList <PwEntry> allUsers            = m_database.GetAllUserNodes();
            PwObjectList <PwEntry> allUserGroupProxies = new PwObjectList <PwEntry>();
            HashSet <string>       proxyIds            = new HashSet <string>();

            //fill the allUserGroupProxyList with relevant entries
            foreach (PwEntry entry in usersGroup.GetEntries(false))
            {
                if (entry.IsProxyNode())
                {
                    allUserGroupProxies.Add(entry);
                    proxyIds.Add(entry.Strings.ReadSafe(KeeShare.UuidLinkField));
                }
            }

            //compare the numbers of proxyNodes in the usersGroup and the entries in the allUsers list
            //if they are equal all relevant proxies exist because we checked there consistence before
            // and we are done here
            Changes changeFlag = Changes.None;

            if (allUsers.UCount != allUserGroupProxies.UCount)
            {
                foreach (PwEntry rootNode in allUsers)
                {
                    if (!proxyIds.Contains(rootNode.Uuid.ToHexString()))
                    {
                        PwEntry proxy = rootNode.CreateProxyNode();
                        proxy.SetParent(usersGroup);
                        changeFlag |= Changes.EntryCreated;
                    }
                }
            }
            return(changeFlag);
        }
        /// <summary>
        /// The function creates a new PwEntry that represents an import source.
        /// </summary>
        /// <param name="fileName">The path to the file in your filesystem that should be used
        /// as import source.</param>
        /// <returns><c>ChangeFlags.CommonChange</c> if the function has made any changes to the 
        /// actual database structure (means if we added he impSrc). 0 if the impSrc allready exists
        /// and we don't have to make any changes anymore.</returns>
        public Changes AddImportPath(string fileName)
        {
            //is it a valid path to a file?
            if (!File.Exists(fileName))
            {
                return Changes.None;
            }
            //check if allready exists
            PwGroup importGroup = m_database.GetImportGroup();
            foreach ( PwEntry src in importGroup.GetEntries( true ) )
            {
                if( src.Strings.ReadSafe(KeeShare.SourcePathField ) == fileName )
                {
                    return Changes.None;
                }
            }

            PwGroup newImportGroup = new PwGroup( true, true, fileName, PwIcon.NetworkServer );
            newImportGroup.SetParent(importGroup );
            PwEntry newImportSource = new PwEntry( true, true );
            newImportSource.Strings.Set(KeeShare.TitleField, new ProtectedString( false, fileName ) );
            newImportSource.Strings.Set(KeeShare.SourcePathField, new ProtectedString( false, fileName ) );
            newImportSource.SetParent(newImportGroup);
            return Changes.GroupCreated;
        }