Пример #1
0
        private static bool CheckPermissionsInWindows(INopFileProvider fileProvider, string path, bool checkRead, bool checkWrite, bool checkModify, bool checkDelete)
        {
            var permissionsAreGranted = true;

            try
            {
                if (!(fileProvider.FileExists(path) || fileProvider.DirectoryExists(path)))
                {
                    return(true);
                }

                var current = WindowsIdentity.GetCurrent();

                var readIsDeny   = false;
                var writeIsDeny  = false;
                var modifyIsDeny = false;
                var deleteIsDeny = false;

                var readIsAllow   = false;
                var writeIsAllow  = false;
                var modifyIsAllow = false;
                var deleteIsAllow = false;

                var rules = fileProvider.GetAccessControl(path).GetAccessRules(true, true, typeof(SecurityIdentifier))
                            .Cast <FileSystemAccessRule>()
                            .ToList();

                foreach (var rule in rules.Where(rule => current.User?.Equals(rule.IdentityReference) ?? false))
                {
                    CheckAccessRule(rule, ref deleteIsDeny, ref modifyIsDeny, ref readIsDeny, ref writeIsDeny, ref deleteIsAllow, ref modifyIsAllow, ref readIsAllow, ref writeIsAllow);
                }

                if (current.Groups != null)
                {
                    foreach (var reference in current.Groups)
                    {
                        foreach (var rule in rules.Where(rule => reference.Equals(rule.IdentityReference)))
                        {
                            CheckAccessRule(rule, ref deleteIsDeny, ref modifyIsDeny, ref readIsDeny, ref writeIsDeny, ref deleteIsAllow, ref modifyIsAllow, ref readIsAllow, ref writeIsAllow);
                        }
                    }
                }

                deleteIsAllow = !deleteIsDeny && deleteIsAllow;
                modifyIsAllow = !modifyIsDeny && modifyIsAllow;
                readIsAllow   = !readIsDeny && readIsAllow;
                writeIsAllow  = !writeIsDeny && writeIsAllow;

                if (checkRead)
                {
                    permissionsAreGranted = readIsAllow;
                }

                if (checkWrite)
                {
                    permissionsAreGranted = permissionsAreGranted && writeIsAllow;
                }

                if (checkModify)
                {
                    permissionsAreGranted = permissionsAreGranted && modifyIsAllow;
                }

                if (checkDelete)
                {
                    permissionsAreGranted = permissionsAreGranted && deleteIsAllow;
                }
            }
            catch (IOException)
            {
                return(false);
            }
            catch
            {
                return(true);
            }

            return(permissionsAreGranted);
        }