Ejemplo n.º 1
0
        /// <summary>
        /// Initializes the stream.
        /// </summary>
        /// <param name="type">The access type.</param>
        private void InitializeStream(StreamAccessType type)
        {
            if (_accessType != StreamAccessType.Undefined)
            {
                return;
            }

            switch (type)
            {
            case StreamAccessType.Readable:
                _accessType = StreamAccessType.Readable;
                _length     = _baseStream.Length;
                break;

            case StreamAccessType.Writable:
                _accessType = StreamAccessType.Writable;
                _length     = 0;
                break;
            }

            _position = 0;
            _buffers  = new Dictionary <Int32, Byte[]>();

            if (_isSingleAccess)
            {
                _bitFlags = new Dictionary <Int32, Byte[]>();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the length of the current stream.
        /// </summary>
        /// <param name="value">The desired length of the current stream in bytes.</param>
        /// <exception cref="System.ObjectDisposedException">Method was called after the stream was closed.</exception>
        /// <exception cref="System.InvalidOperationException">The stream is not writable or does not support seeking.</exception>
        public override void SetLength(Int64 value)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Method was called after the stream was closed.");
            }

            if (_accessType == StreamAccessType.Readable)
            {
                throw new InvalidOperationException("The stream is not writable.");
            }

            if (_accessType == StreamAccessType.Undefined)
            {
                _accessType = StreamAccessType.Writable;
            }

            if (_length < value)
            {
                CreateBuffer(_length, value - _length);
            }

            if (_length > value)
            {
                RemoveBuffer(value, _length - value);
            }

            _length = value;
        }
Ejemplo n.º 3
0
 public WriteEvents(
     IPublisher publisher,
     TimeSpan timeout,
     IEnvelope clientResponseEnvelope,
     Guid internalCorrId,
     Guid clientCorrId,
     string streamId,
     bool betterOrdering,
     long expectedVersion,
     ClaimsPrincipal user,
     Event[] events,
     CommitSource commitSource)
     : base(
         publisher,
         timeout,
         clientResponseEnvelope,
         internalCorrId,
         clientCorrId,
         expectedVersion,
         commitSource,
         prepareCount: 0,
         waitForCommit: true)
 {
     _streamId = streamId;
     //this seems like it should work, but really really doesn't
     //TODO(clc): confirm MetaWrite is implemented
     //_accessType = SystemStreams.IsMetaStream(streamId) ? StreamAccessType.MetaWrite : StreamAccessType.Write;
     _accessType     = StreamAccessType.Write;
     _betterOrdering = betterOrdering;
     _user           = user;
     _events         = events;
 }
Ejemplo n.º 4
0
 StreamAccess IIndexReader.CheckStreamAccess(string streamId, StreamAccessType streamAccessType, IPrincipal user)
 {
     Ensure.NotNullOrEmpty(streamId, "streamId");
     using (var reader = _cache.BorrowReader())
     {
         return(CheckStreamAccessInternal(reader, streamId, streamAccessType, user));
     }
 }
Ejemplo n.º 5
0
 public CheckStreamAccessCompleted(Guid correlationId, string eventStreamId, long?transactionId,
                                   StreamAccessType accessType, StreamAccess accessResult)
 {
     CorrelationId = correlationId;
     EventStreamId = eventStreamId;
     TransactionId = transactionId;
     AccessType    = accessType;
     AccessResult  = accessResult;
 }
Ejemplo n.º 6
0
            public CheckStreamAccess(IEnvelope envelope, Guid correlationId, string eventStreamId, long?transactionId,
                                     StreamAccessType accessType, IPrincipal user)
                : base(correlationId, correlationId, envelope, user)
            {
                if (eventStreamId == null && transactionId == null)
                {
                    throw new ArgumentException("Neither eventStreamId nor transactionId is specified.");
                }

                EventStreamId = eventStreamId;
                TransactionId = transactionId;
                AccessType    = accessType;
            }
Ejemplo n.º 7
0
            public CheckStreamAccess(IEnvelope envelope, Guid correlationId, string eventStreamId, long?transactionId,
                                     StreamAccessType accessType, IPrincipal user, bool singleAffinity = false)
                : base(correlationId, correlationId, envelope, user)
            {
                if (eventStreamId == null && transactionId == null)
                {
                    throw new ArgumentException("Neither eventStreamId nor transactionId is specified.");
                }
                EventStreamId = eventStreamId;
                TransactionId = transactionId;
                var hash = String.IsNullOrEmpty(EventStreamId) ? TransactionId.GetHashCode() : EventStreamId.GetHashCode();

                _affinity  = singleAffinity ? 1 : hash;
                AccessType = accessType;
            }
Ejemplo n.º 8
0
        protected void InitNoPreparePhase(IEnvelope responseEnvelope, Guid internalCorrId, Guid clientCorrId,
                                          string eventStreamId, IPrincipal user, StreamAccessType accessType)
        {
            if (_initialized)
            {
                throw new InvalidOperationException();
            }

            _initialized = true;

            _responseEnvelope = responseEnvelope;
            _internalCorrId   = internalCorrId;
            _clientCorrId     = clientCorrId;

            _nextTimeoutTime = DateTime.UtcNow + CommitTimeout;
            _awaitingPrepare = 0;
            Publisher.Publish(new StorageMessage.CheckStreamAccess(
                                  PublishEnvelope, internalCorrId, eventStreamId, null, accessType, user, _betterOrdering));
        }
Ejemplo n.º 9
0
        protected void InitTwoPhase(IEnvelope responseEnvelope, Guid internalCorrId, Guid clientCorrId,
                                    long transactionId, IPrincipal user, StreamAccessType accessType)
        {
            if (_initialized)
            {
                throw new InvalidOperationException();
            }

            _initialized = true;

            _responseEnvelope = responseEnvelope;
            _internalCorrId   = internalCorrId;
            _clientCorrId     = clientCorrId;
            _transactionId    = transactionId;

            _nextTimeoutTime = DateTime.UtcNow + PrepareTimeout;

            Publisher.Publish(new StorageMessage.CheckStreamAccess(
                                  PublishEnvelope, internalCorrId, null, transactionId, accessType, user));
        }
Ejemplo n.º 10
0
        private StreamAccess CheckStreamAccessInternal(TFReaderLease reader, string streamId,
                                                       StreamAccessType streamAccessType, IPrincipal user)
        {
            if (SystemStreams.IsMetastream(streamId))
            {
                switch (streamAccessType)
                {
                case StreamAccessType.Read:
                    return(CheckStreamAccessInternal(reader, SystemStreams.OriginalStreamOf(streamId), StreamAccessType.MetaRead, user));

                case StreamAccessType.Write:
                    return(CheckStreamAccessInternal(reader, SystemStreams.OriginalStreamOf(streamId), StreamAccessType.MetaWrite, user));

                case StreamAccessType.Delete:
                case StreamAccessType.MetaRead:
                case StreamAccessType.MetaWrite:
                    return(new StreamAccess(false));

                default:
                    throw new ArgumentOutOfRangeException("streamAccessType");
                }
            }

            if ((streamAccessType == StreamAccessType.Write || streamAccessType == StreamAccessType.Delete) &&
                streamId == SystemStreams.AllStream)
            {
                return(new StreamAccess(false));
            }

            var       sysSettings = _cache.GetSystemSettings() ?? SystemSettings.Default;
            var       meta        = GetStreamMetadataCached(reader, streamId);
            StreamAcl acl;
            StreamAcl sysAcl;
            StreamAcl defAcl;

            if (SystemStreams.IsSystemStream(streamId))
            {
                defAcl = SystemSettings.Default.SystemStreamAcl;
                sysAcl = sysSettings.SystemStreamAcl ?? defAcl;
                acl    = meta.Acl ?? sysAcl;
            }
            else
            {
                defAcl = SystemSettings.Default.UserStreamAcl;
                sysAcl = sysSettings.UserStreamAcl ?? defAcl;
                acl    = meta.Acl ?? sysAcl;
            }
            string[] roles;
            switch (streamAccessType)
            {
            case StreamAccessType.Read: roles = acl.ReadRoles ?? sysAcl.ReadRoles ?? defAcl.ReadRoles; break;

            case StreamAccessType.Write: roles = acl.WriteRoles ?? sysAcl.WriteRoles ?? defAcl.WriteRoles; break;

            case StreamAccessType.Delete: roles = acl.DeleteRoles ?? sysAcl.DeleteRoles ?? defAcl.DeleteRoles; break;

            case StreamAccessType.MetaRead: roles = acl.MetaReadRoles ?? sysAcl.MetaReadRoles ?? defAcl.MetaReadRoles; break;

            case StreamAccessType.MetaWrite: roles = acl.MetaWriteRoles ?? sysAcl.MetaWriteRoles ?? defAcl.MetaWriteRoles; break;

            default: throw new ArgumentOutOfRangeException("streamAccessType");
            }

            var isPublic = roles.Contains(x => x == SystemRoles.All);

            if (isPublic)
            {
                return(new StreamAccess(true, true));
            }
            if (user == null)
            {
                return(new StreamAccess(false));
            }
            if (user.IsInRole(SystemRoles.Admins))
            {
                return(new StreamAccess(true));
            }
            for (int i = 0; i < roles.Length; ++i)
            {
                if (user.IsInRole(roles[i]))
                {
                    return(new StreamAccess(true));
                }
            }
            return(new StreamAccess(false));
        }
Ejemplo n.º 11
0
 StreamAccessResult IReadIndex.CheckStreamAccess(string streamId, StreamAccessType streamAccessType, IPrincipal user)
 {
     var reader = _readers.Get();
     try
     {
         return CheckStreamAccessInternal(reader, streamId, streamAccessType, user);
     }
     finally
     {
         _readers.Return(reader);
     }
 }
Ejemplo n.º 12
0
        private StreamAccess CheckStreamAccessInternal(TFReaderLease reader, string streamId,
                                                       StreamAccessType streamAccessType, IPrincipal user)
        {
            if (SystemStreams.IsMetastream(streamId))
            {
                switch (streamAccessType)
                {
                    case StreamAccessType.Read:
                        return CheckStreamAccessInternal(reader, SystemStreams.OriginalStreamOf(streamId), StreamAccessType.MetaRead, user);
                    case StreamAccessType.Write:
                        return CheckStreamAccessInternal(reader, SystemStreams.OriginalStreamOf(streamId), StreamAccessType.MetaWrite, user);
                    case StreamAccessType.Delete:
                    case StreamAccessType.MetaRead:
                    case StreamAccessType.MetaWrite:
                        return new StreamAccess(false);
                    default:
                        throw new ArgumentOutOfRangeException("streamAccessType");
                }
            }

            if ((streamAccessType == StreamAccessType.Write || streamAccessType == StreamAccessType.Delete)
                && streamId == SystemStreams.AllStream)
                return new StreamAccess(false);

            var sysSettings = _backend.GetSystemSettings() ?? SystemSettings.Default;
            var meta = GetStreamMetadataCached(reader, streamId);
            StreamAcl acl;
            StreamAcl sysAcl;
            StreamAcl defAcl;
            if (SystemStreams.IsSystemStream(streamId))
            {
                defAcl = SystemSettings.Default.SystemStreamAcl;
                sysAcl = sysSettings.SystemStreamAcl ?? defAcl;
                acl = meta.Acl ?? sysAcl;
            }
            else
            {
                defAcl = SystemSettings.Default.UserStreamAcl;
                sysAcl = sysSettings.UserStreamAcl ?? defAcl;
                acl = meta.Acl ?? sysAcl;
            }
            string[] roles;
            switch (streamAccessType)
            {
                case StreamAccessType.Read: roles = acl.ReadRoles ?? sysAcl.ReadRoles ?? defAcl.ReadRoles; break;
                case StreamAccessType.Write: roles = acl.WriteRoles ?? sysAcl.WriteRoles ?? defAcl.WriteRoles; break;
                case StreamAccessType.Delete: roles = acl.DeleteRoles ?? sysAcl.DeleteRoles ?? defAcl.DeleteRoles; break;
                case StreamAccessType.MetaRead: roles = acl.MetaReadRoles ?? sysAcl.MetaReadRoles ?? defAcl.MetaReadRoles; break;
                case StreamAccessType.MetaWrite: roles = acl.MetaWriteRoles ?? sysAcl.MetaWriteRoles ?? defAcl.MetaWriteRoles; break;
                default: throw new ArgumentOutOfRangeException("streamAccessType");
            }

            var isPublic = roles.Contains(x => x == SystemRoles.All);
            if (isPublic) return new StreamAccess(true, true);
            if (user == null) return new StreamAccess(false);
            if (user.IsInRole(SystemRoles.Admins)) return new StreamAccess(true);
            for (int i = 0; i < roles.Length; ++i)
            {
                if (user.IsInRole(roles[i]))
                    return new StreamAccess(true);
            }
            return new StreamAccess(false);
        }
Ejemplo n.º 13
0
 StreamAccess IIndexReader.CheckStreamAccess(string streamId, StreamAccessType streamAccessType, IPrincipal user)
 {
     Ensure.NotNullOrEmpty(streamId, "streamId");
     using (var reader = _backend.BorrowReader())
     {
         return CheckStreamAccessInternal(reader, streamId, streamAccessType, user);
     }
 }
Ejemplo n.º 14
0
 public StreamAccess CheckStreamAccess(string streamId, StreamAccessType streamAccessType, IPrincipal user)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 15
0
 public CheckStreamAccessCompleted(Guid correlationId, string eventStreamId, long? transactionId, 
                                   StreamAccessType accessType, StreamAccess accessResult)
 {
     CorrelationId = correlationId;
     EventStreamId = eventStreamId;
     TransactionId = transactionId;
     AccessType = accessType;
     AccessResult = accessResult;
 }
Ejemplo n.º 16
0
 StreamAccess IReadIndex.CheckStreamAccess(string streamId, StreamAccessType streamAccessType, IPrincipal user)
 {
     return(_indexReader.CheckStreamAccess(streamId, streamAccessType, user));
 }
Ejemplo n.º 17
0
        private StreamAccessResult CheckStreamAccessInternal(ITransactionFileReader reader, string streamId, 
                                                             StreamAccessType streamAccessType, IPrincipal user)
        {
            if (SystemStreams.IsMetastream(streamId))
            {
                switch (streamAccessType)
                {
                    case StreamAccessType.Read:
                        return CheckStreamAccessInternal(reader, SystemStreams.OriginalStreamOf(streamId), StreamAccessType.MetaRead, user);
                    case StreamAccessType.Write:
                        return CheckStreamAccessInternal(reader, SystemStreams.OriginalStreamOf(streamId), StreamAccessType.MetaWrite, user);
                    case StreamAccessType.Delete:
                    case StreamAccessType.MetaRead:
                    case StreamAccessType.MetaWrite:
                        return StreamAccessResult.Denied;
                    default:
                        throw new ArgumentOutOfRangeException("streamAccessType");
                }
            }

            if ((streamAccessType == StreamAccessType.Write || streamAccessType == StreamAccessType.Delete)
                && streamId == SystemStreams.AllStream)
                return StreamAccessResult.Denied;

            if (user != null && user.IsInRole(SystemUserGroups.Admins))
                return StreamAccessResult.Granted;

            var meta = GetStreamMetadataCached(reader, streamId);
            var isSystemStream = SystemStreams.IsSystemStream(streamId);
            switch (streamAccessType)
            {
                case StreamAccessType.Read: 
                    return CheckRoleAccess(meta.Acl == null ? null : meta.Acl.ReadRole, user, isSystemStream);
                case StreamAccessType.Write:
                    return CheckRoleAccess(meta.Acl == null ? null : meta.Acl.WriteRole, user, isSystemStream);
                case StreamAccessType.Delete:
                    return CheckRoleAccess(meta.Acl == null ? null : meta.Acl.DeleteRole, user, isSystemStream);
                case StreamAccessType.MetaRead:
                    return CheckRoleAccess(meta.Acl == null ? null : meta.Acl.MetaReadRole, user, isSystemStream);
                case StreamAccessType.MetaWrite:
                    return CheckRoleAccess(meta.Acl == null ? null : meta.Acl.MetaWriteRole, user, isSystemStream);
                default: throw new ArgumentOutOfRangeException("streamAccessType");
            }
        }
Ejemplo n.º 18
0
            public CheckStreamAccess(IEnvelope envelope, Guid correlationId, string eventStreamId, long? transactionId, 
                                     StreamAccessType accessType, IPrincipal user)
                : base(correlationId, correlationId, envelope, user)
            {
                if (eventStreamId == null && transactionId == null)
                    throw new ArgumentException("Neither eventStreamId nor transactionId is specified.");

                EventStreamId = eventStreamId;
                TransactionId = transactionId;
                AccessType = accessType;
            }
Ejemplo n.º 19
0
 StreamAccess IReadIndex.CheckStreamAccess(string streamId, StreamAccessType streamAccessType, IPrincipal user)
 {
     return _indexReader.CheckStreamAccess(streamId, streamAccessType, user);
 }
Ejemplo n.º 20
0
 public StreamAccess CheckStreamAccess(string streamId, StreamAccessType streamAccessType, ClaimsPrincipal user)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 21
0
 public CheckStreamAccess(IEnvelope envelope, Guid correlationId, string eventStreamId, long? transactionId,
                          StreamAccessType accessType, IPrincipal user, bool singleAffinity = false)
     : base(correlationId, correlationId, envelope, user)
 {
     if (eventStreamId == null && transactionId == null)
         throw new ArgumentException("Neither eventStreamId nor transactionId is specified.");
     EventStreamId = eventStreamId;
     TransactionId = transactionId;
     var hash = String.IsNullOrEmpty(EventStreamId) ? TransactionId.GetHashCode() : EventStreamId.GetHashCode();
     _affinity = singleAffinity ? 1 : hash;
     AccessType = accessType;
 }