Exemple #1
0
        /// <summary>
        /// Delete the specified identity
        /// </summary>
        public void DeleteIdentity(string userName, IPrincipal authContext)
        {
            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException(nameof(userName));
            }

            this.m_traceSource.TraceInformation("Delete identity {0}", userName);
            try
            {
                // submit the changes
                using (var dataContext = new ModelDataContext(this.m_configuration.ReadWriteConnectionString))
                {
                    new PolicyPermission(System.Security.Permissions.PermissionState.Unrestricted, PermissionPolicyIdentifiers.UnrestrictedAdministration, authContext).Demand();

                    var user = dataContext.SecurityUsers.FirstOrDefault(o => o.UserName == userName);
                    if (user == null)
                    {
                        throw new KeyNotFoundException("Specified user does not exist!");
                    }

                    // Obsolete
                    user.ObsoletionTime    = DateTimeOffset.Now;
                    user.ObsoletedByEntity = authContext.GetUser(dataContext);
                    user.SecurityStamp     = Guid.NewGuid().ToString();

                    dataContext.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                this.m_traceSource.TraceEvent(TraceEventType.Error, e.HResult, e.ToString());
                throw;
            }
        }
        /// <summary>
        /// Perform the actual update.
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="data">Data.</param>
        public override TModel Update(ModelDataContext context, TModel data, IPrincipal principal)
        {
            // Check for key
            if (data.Key == Guid.Empty)
            {
                throw new SqlFormalConstraintException(SqlFormalConstraintType.NonIdentityUpdate);
            }

            // Get current object
            var domainObject  = this.FromModelInstance(data, context, principal) as TDomain;
            var currentObject = context.GetTable <TDomain>().FirstOrDefault(ExpressionRewriter.Rewrite <TDomain>(o => o.Id == data.Key));

            // Not found
            if (currentObject == null)
            {
                throw new KeyNotFoundException(data.Key.ToString());
            }

            // VObject
            var vobject = domainObject as IDbNonVersionedBaseData;

            if (vobject != null)
            {
                vobject.UpdatedBy   = principal.GetUser(context).UserId;
                vobject.UpdatedTime = DateTimeOffset.Now;
            }

            currentObject.CopyObjectData(domainObject);
            context.SubmitChanges();

            return(this.ToModelInstance(domainObject, context, principal));
        }
Exemple #3
0
        public User GetUserFromPrincipal(IPrincipal principal)
        {
            if (principal == null)
            {
                throw new ArgumentNullException(nameof(principal));
            }

            return(principal.GetUser());
        }
Exemple #4
0
        /// <summary>
        /// Update the data with new version information
        /// </summary>
        public override TModel Update(ModelDataContext context, TModel data, IPrincipal principal)
        {
            if (data.Key == Guid.Empty)
            {
                throw new SqlFormalConstraintException(SqlFormalConstraintType.NonIdentityUpdate);
            }

            // This is technically an insert and not an update
            var existingObject = context.GetTable <TDomain>().FirstOrDefault(ExpressionRewriter.Rewrite <TDomain>(o => o.Id == data.Key && !o.ObsoletionTime.HasValue)); // Get the last version (current)

            if (existingObject == null)
            {
                throw new KeyNotFoundException(data.Key.ToString());
            }
            else if (existingObject.IsReadonly)
            {
                throw new SqlFormalConstraintException(SqlFormalConstraintType.UpdatedReadonlyObject);
            }

            // Map existing
            var storageInstance = this.FromModelInstance(data, context, principal);

            // Create a new version
            var user             = principal.GetUser(context);
            var newEntityVersion = new TDomain();

            newEntityVersion.CopyObjectData(storageInstance);

            // Client did not change on update, so we need to update!!!
            if (!data.VersionKey.HasValue ||
                data.VersionKey.Value == existingObject.VersionId)
            {
                data.VersionKey = newEntityVersion.VersionId = Guid.NewGuid();
            }

            data.VersionSequence    = newEntityVersion.VersionSequenceId = default(Decimal);
            newEntityVersion.Id     = data.Key.Value;
            data.PreviousVersionKey = newEntityVersion.ReplacesVersionId = existingObject.VersionId;
            data.CreatedByKey       = newEntityVersion.CreatedBy = user.UserId;
            // Obsolete the old version
            existingObject.ObsoletedBy    = user.UserId;
            existingObject.ObsoletionTime = DateTime.Now;
            context.GetTable <TDomain>().InsertOnSubmit(newEntityVersion);
            context.SubmitChanges();

            // Pull database generated fields
            data.VersionSequence = newEntityVersion.VersionSequenceId;
            data.CreationTime    = newEntityVersion.CreationTime;

            return(data);
            //return base.Update(context, data, principal);
        }
        /// <summary>
        /// Performthe actual insert.
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="data">Data.</param>
        public override TModel Insert(ModelDataContext context, TModel data, IPrincipal principal)
        {
            var domainObject = this.FromModelInstance(data, context, principal) as TDomain;

            if (domainObject.Id == Guid.Empty)
            {
                data.Key = domainObject.Id = Guid.NewGuid();
            }

            // Ensure created by exists
            data.CreatedBy?.EnsureExists(context, principal);
            data.CreatedByKey = domainObject.CreatedBy = domainObject.CreatedBy == Guid.Empty ? principal.GetUser(context).UserId : domainObject.CreatedBy;
            context.GetTable <TDomain>().InsertOnSubmit(domainObject);

            context.SubmitChanges();
            data.CreationTime = (DateTimeOffset)domainObject.CreationTime;

            return(this.ToModelInstance(domainObject, context, principal));
        }
        /// <summary>
        /// Performs the actual obsoletion
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="data">Data.</param>
        public override TModel Obsolete(ModelDataContext context, TModel data, IPrincipal principal)
        {
            if (data.Key == Guid.Empty)
            {
                throw new SqlFormalConstraintException(SqlFormalConstraintType.NonIdentityUpdate);
            }

            // Current object
            var currentObject = context.GetTable <TDomain>().FirstOrDefault(ExpressionRewriter.Rewrite <TDomain>(o => o.Id == data.Key));

            if (currentObject == null)
            {
                throw new KeyNotFoundException(data.Key.ToString());
            }

            data.ObsoletedBy?.EnsureExists(context, principal);
            data.ObsoletedByKey = currentObject.ObsoletedBy = data.ObsoletedBy?.Key ?? principal.GetUser(context).UserId;
            data.ObsoletionTime = currentObject.ObsoletionTime = currentObject.ObsoletionTime ?? DateTimeOffset.Now;
            context.SubmitChanges();
            return(data);
        }
Exemple #7
0
        /// <summary>
        /// Insert the data
        /// </summary>
        public override TModel Insert(ModelDataContext context, TModel data, IPrincipal principal)
        {
            // first we map the TDataKey entity
            var nonVersionedPortion = m_mapper.MapModelInstance <TModel, TDomainKey>(data);

            // Domain object
            var domainObject = this.FromModelInstance(data, context, principal) as TDomain;

            domainObject.NonVersionedObject = nonVersionedPortion;
            if (nonVersionedPortion.Id == Guid.Empty &&
                domainObject.Id != Guid.Empty)
            {
                nonVersionedPortion.Id = domainObject.Id;
            }

            if (nonVersionedPortion.Id == null ||
                nonVersionedPortion.Id == Guid.Empty)
            {
                data.Key = Guid.NewGuid();
                nonVersionedPortion.Id = data.Key.Value;
            }
            if (domainObject.VersionId == null ||
                domainObject.VersionId == Guid.Empty)
            {
                data.VersionKey        = Guid.NewGuid();
                domainObject.VersionId = data.VersionKey.Value;
            }

            // Ensure created by exists
            data.CreatedBy?.EnsureExists(context, principal);
            data.CreatedByKey = domainObject.CreatedBy = domainObject.CreatedBy == Guid.Empty ? principal.GetUser(context).UserId : domainObject.CreatedBy;
            context.GetTable <TDomain>().InsertOnSubmit(domainObject);

            context.SubmitChanges();

            data.VersionSequence = domainObject.VersionSequenceId;
            data.VersionKey      = domainObject.VersionId;
            data.Key             = domainObject.Id;
            data.CreationTime    = (DateTimeOffset)domainObject.CreationTime;

            return(data);
        }