private void SetupSystem()
        {
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features)
                            {
                                try {
                                    feature.OnSystemEvent(SystemEventType.TableCompositeSetup, query);
                                } catch (Exception ex) {
                                    query.Context.OnError(
                                        new Exception(
                                            String.Format("The feature {0} (v{1}) caused an error on Composite-Setup event.", feature.FeatureName,
                                                          feature.Version), ex));
                                    throw;
                                }
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception initializing the system.", e);
                }
            }
        }
Example #2
0
 public bool DropRole(string roleName)
 {
     try {
         return(SystemSession.Access().UserManager.DropRole(roleName));
     } finally {
         RevokeAllGrantsFrom(roleName);
     }
 }
Example #3
0
 public void Revoke(DbObjectType objectType, ObjectName objectName, string grantee, Privileges privileges,
                    bool grantOption = false)
 {
     try {
         var revoker = Session.User.Name;
         var grant   = new Grant(privileges, objectName, objectType, grantee, revoker, grantOption);
         SystemSession.Access().PrivilegeManager.Revoke(grant);
     } finally {
         var key = new GrantCacheKey(grantee, objectType, objectName.FullName, grantOption, false);
         PrivilegesCache.Remove(key);
     }
 }
Example #4
0
        public bool DeleteUser(string userName)
        {
            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("userName");
            }

            try {
                return(SystemSession.Access().UserManager.DropUser(userName));
            } finally {
                RevokeAllGrantsFrom(userName);
            }
        }
Example #5
0
        public void CreateUser(string userName, string identification, string token)
        {
            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("userName");
            }
            if (String.IsNullOrEmpty(identification))
            {
                throw new ArgumentNullException("identification");
            }
            if (String.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            if (String.Equals(userName, User.PublicName, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                          String.Format("User name '{0}' is reserved and cannot be registered.", User.PublicName), "userName");
            }

            if (userName.Length <= 1)
            {
                throw new ArgumentException("User name must be at least one character.");
            }
            if (token.Length <= 1)
            {
                throw new ArgumentException("The password must be at least one character.");
            }

            var c = userName[0];

            if (c == '#' || c == '@' || c == '$' || c == '&')
            {
                throw new ArgumentException(
                          String.Format("User name '{0}' is invalid: cannot start with '{1}' character.", userName, c), "userName");
            }

            var identifier = FindIdentifier(identification);

            if (identifier == null)
            {
                throw new ArgumentException(String.Format("User identification method '{0}' cannot be found", identification));
            }

            var userId   = identifier.CreateIdentification(token);
            var userInfo = new UserInfo(userName, userId);

            SystemSession.Access().UserManager.CreateUser(userInfo);
        }
        private void CreateSystem()
        {
            // Create the transaction
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features)
                            {
                                feature.OnSystemEvent(SystemEventType.TableCompositeCreate, query);
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception creating composite.", e);
                }
            }
        }
Example #7
0
        public bool Authenticate(string username, string password)
        {
            try {
                if (String.IsNullOrEmpty(username))
                {
                    throw new ArgumentNullException("username");
                }
                if (String.IsNullOrEmpty(password))
                {
                    throw new ArgumentNullException("password");
                }

                var userInfo = SystemSession.Access().UserManager.GetUser(username);

                if (userInfo == null)
                {
                    return(false);
                }

                var userId     = userInfo.Identification;
                var identifier = FindIdentifier(userId.Method);

                if (identifier == null)
                {
                    throw new SecurityException(String.Format("The user '{0}' was identified by '{1}' but the identifier cannot be found in the context.", userInfo.Name, userId.Method));
                }

                if (!identifier.VerifyIdentification(password, userId))
                {
                    return(false);
                }

                // Successfully authenticated...
                return(true);
            } catch (SecurityException) {
                throw;
            } catch (Exception ex) {
                throw new SecurityException("Could not authenticate user.", ex);
            }
        }
Example #8
0
        public void AlterUserPassword(string username, string identification, string token)
        {
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username");
            }
            if (String.IsNullOrEmpty(identification))
            {
                throw new ArgumentNullException("identification");
            }

            var identifier = FindIdentifier(identification);

            if (identifier == null)
            {
                throw new ArgumentException(String.Format("User identification method '{0}' cannot be found", identification));
            }

            var userId   = identifier.CreateIdentification(token);
            var userInfo = new UserInfo(username, userId);

            SystemSession.Access().UserManager.AlterUser(userInfo);
        }
Example #9
0
        private void SetupSystem()
        {
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features) {
                                try {
                                    feature.OnSystemEvent(SystemEventType.TableCompositeSetup, query);
                                } catch (Exception ex) {
                                    query.Context.OnError(
                                        new Exception(
                                            String.Format("The feature {0} (v{1}) caused an error on Composite-Setup event.", feature.FeatureName,
                                                feature.Version), ex));
                                    throw;
                                }
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception initializing the system.", e);
                }
            }
        }
Example #10
0
 public void CreateRoutine(RoutineInfo routineInfo)
 {
     SystemSession.Access().CreateObject(routineInfo);
 }
Example #11
0
 public bool UserExists(string userName)
 {
     return(SystemSession.Access().UserManager.UserExists(userName));
 }
Example #12
0
 public void CreateUser(UserInfo userInfo)
 {
     SystemSession.Access().UserManager.CreateUser(userInfo);
 }
Example #13
0
 public void SetUserStatus(string username, UserStatus status)
 {
     SystemSession.Access().UserManager.SetUserStatus(username, status);
 }
Example #14
0
 public UserStatus GetUserStatus(string userName)
 {
     return(SystemSession.Access().UserManager.GetUserStatus(userName));
 }
Example #15
0
 internal static IQuery Direct(this IQuery query)
 {
     var systemSession = new SystemSession(query.Session.Transaction, query.Session.CurrentSchema);
     return new Query(systemSession);
 }
Example #16
0
 public void SetRoleAdmin(string roleName, string userName)
 {
     SystemSession.Access().UserManager.SetRoleAdmin(roleName, userName);
 }
Example #17
0
 public bool RoleExists(string roleName)
 {
     return(SystemSession.Access().UserManager.RoleExists(roleName));
 }
Example #18
0
        private void CreateSystem()
        {
            // Create the transaction
            using (var transaction = Database.CreateSafeTransaction(IsolationLevel.Serializable)) {
                try {
                    using (var session = new SystemSession(transaction, SystemSchema.Name)) {
                        using (var query = session.CreateQuery()) {
                            foreach (var feature in Database.System.Features) {
                                feature.OnSystemEvent(SystemEventType.TableCompositeCreate, query);
                            }
                        }
                    }

                    // Commit and close the transaction.
                    transaction.Commit();
                } catch (TransactionException e) {
                    throw new InvalidOperationException("Transaction Exception creating composite.", e);
                }
            }
        }
        public static void CheckAddConstraintViolations(this ITransaction transaction, ITable table, int[] rowIndices,
			ConstraintDeferrability deferred)
        {
            string curSchema = table.TableInfo.TableName.Parent.Name;
            using (var session = new SystemSession(transaction, curSchema)) {
                using (var queryContext = session.CreateQuery()) {

                    // Quick exit case
                    if (rowIndices == null || rowIndices.Length == 0)
                        return;

                    var tableInfo = table.TableInfo;
                    var tableName = tableInfo.TableName;

                    // ---- Constraint checking ----

                    // Check any primary key constraint.
                    var primaryKey = transaction.QueryTablePrimaryKey(tableName);
                    if (primaryKey != null &&
                        (deferred == ConstraintDeferrability.InitiallyDeferred ||
                         primaryKey.Deferred == ConstraintDeferrability.InitiallyImmediate)) {

                        // For each row added to this column
                        foreach (int rowIndex in rowIndices) {
                            if (!IsUniqueColumns(table, rowIndex, primaryKey.ColumnNames, false)) {
                                throw new PrimaryKeyViolationException(tableName, primaryKey.ConstraintName, primaryKey.ColumnNames, deferred);
                            }
                        } // For each row being added
                    }

                    // Check any unique constraints.
                    var uniqueConstraints = transaction.QueryTableUniqueKeys(tableName);
                    foreach (var unique in uniqueConstraints) {
                        if (deferred == ConstraintDeferrability.InitiallyDeferred ||
                            unique.Deferred == ConstraintDeferrability.InitiallyImmediate) {

                            // For each row added to this column
                            foreach (int rowIndex in rowIndices) {
                                if (!IsUniqueColumns(table, rowIndex, unique.ColumnNames, true)) {
                                    throw new UniqueKeyViolationException(tableName, unique.ConstraintName, unique.ColumnNames, deferred);
                                }
                            } // For each row being added
                        }
                    }

                    // Check any foreign key constraints.
                    // This ensures all foreign references in the table are referenced
                    // to valid records.
                    var foreignConstraints = transaction.QueryTableForeignKeys(tableName);

                    foreach (var reference in foreignConstraints) {
                        if (deferred == ConstraintDeferrability.InitiallyDeferred ||
                            reference.Deferred == ConstraintDeferrability.InitiallyImmediate) {
                            // For each row added to this column
                            foreach (int rowIndex in rowIndices) {
                                // Make sure the referenced record exists

                                // Return the count of records where the given row of
                                //   table_name(columns, ...) IN
                                //                    ref_table_name(ref_columns, ...)
                                int rowCount = RowCountOfReferenceTable(transaction,
                                    rowIndex,
                                    reference.TableName, reference.ColumnNames,
                                    reference.ForeignTable, reference.ForeignColumnNames,
                                    false);
                                if (rowCount == -1) {
                                    // foreign key is NULL
                                }

                                if (rowCount == 0) {
                                    throw new ForeignKeyViolationException(tableName, reference.ConstraintName, reference.ColumnNames,
                                        reference.ForeignTable, reference.ForeignColumnNames, deferred);
                                }
                            } // For each row being added.
                        }
                    }

                    // Any general checks of the inserted data
                    var checkConstraints = transaction.QueryTableCheckExpressions(tableName);

                    // For each check constraint, check that it evaluates to true.
                    for (int i = 0; i < checkConstraints.Length; ++i) {
                        var check = checkConstraints[i];
                        if (deferred == ConstraintDeferrability.InitiallyDeferred ||
                            check.Deferred == ConstraintDeferrability.InitiallyImmediate) {

                            var exp = tableInfo.ResolveColumns(transaction.IgnoreIdentifiersCase(), check.CheckExpression);

                            // For each row being added to this column
                            for (int rn = 0; rn < rowIndices.Length; ++rn) {
                                var resolver = new TableRowVariableResolver(table, rowIndices[rn]);
                                var evalExp = exp.Evaluate(queryContext, resolver, null);
                                var ob = ((SqlConstantExpression) evalExp).Value;

                                var b = ob.AsBoolean();

                                if (!b.IsNull) {
                                    if (!((SqlBoolean)b.Value)) {
                                        // Evaluated to false so don't allow this row to be added.
                                        throw new CheckViolationException(tableName, check.ConstraintName, check.CheckExpression, deferred);
                                    }
                                } else {
                                    // NOTE: This error will pass the row by default
                                    // TODO: emit a warning
                                }
                            }
                        }
                    }
                }
            }
        }
Example #20
0
 public bool DeleteRoutine(ObjectName routineName)
 {
     return(SystemSession.Access().DropObject(DbObjectType.Routine, routineName));
 }
Example #21
0
        private ITable FindEntry(Table table, ObjectName routineName)
        {
            var schemav = table.GetResolvedColumnName(0);
            var namev = table.GetResolvedColumnName(1);

            using (var session = new SystemSession(transaction)) {
                using (var context = session.CreateQuery()) {
                    var t = table.SimpleSelect(context, namev, SqlExpressionType.Equal,
                        SqlExpression.Constant(DataObject.String(routineName.Name)));
                    t = t.ExhaustiveSelect(context,
                        SqlExpression.Equal(SqlExpression.Reference(schemav),
                            SqlExpression.Constant(DataObject.String(routineName.ParentName))));

                    // This should be at most 1 row in size
                    if (t.RowCount > 1)
                        throw new Exception("Assert failed: multiple procedure names for " + routineName);

                    // Return the entries found.
                    return t;
                }
            }
        }
Example #22
0
 public void CreateRole(string roleName)
 {
     SystemSession.Access().UserManager.CreateRole(roleName);
 }
Example #23
0
        internal static IQuery Direct(this IQuery query)
        {
            var systemSession = new SystemSession(query.Session.Transaction, query.Session.CurrentSchema);

            return(new Query(systemSession));
        }