Esempio n. 1
0
        public override IContext CreateContext(string contextName, ContextSchema schema)
        {
            if (string.IsNullOrEmpty(contextName))
            {
                throw new ArgumentNullException("contextName");
            }
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            IContext context;

            lock (padlock) {
                if (ContextsContains(contextName))
                {
                    throw new ContextAlreadyExistsException("A context with the name [" + contextName + "] already exists.");
                }
                using (var transaction = TransactionFactory.BeginTransaction(CurrentDB)) {
                    CurrentDB.ExecuteNonQuery("dbo.USER_CreateContext",
                                              transaction,
                                              CurrentDB.CreateStringInputParameter("@chvContext", DbType.AnsiString, contextName));
                    foreach (var s in schema)
                    {
                        AddSchemaToContext(s, contextName, transaction);
                    }
                    context = new Context(contextName);
                    transaction.Commit();
                }

                contexts.Add(context);
            }

            return(context);
        }
Esempio n. 2
0
 protected virtual void AddSchemaToContext(PropertyDefinition definition, string contextName, ITransaction transaction)
 {
     CurrentDB.ExecuteNonQuery("dbo.USER_AddContextualPropertySchema",
                               transaction,
                               CurrentDB.CreateStringInputParameter("@chvContext", DbType.AnsiString, contextName, true),
                               CurrentDB.CreateStringInputParameter("@chvPropertyName",
                                                                    DbType.AnsiString,
                                                                    definition.PropertyName,
                                                                    true));
 }
Esempio n. 3
0
 public override void DeleteContext(IContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     lock (padlock) {
         using (var transaction = TransactionFactory.BeginTransaction(CurrentDB)) {
             CurrentDB.ExecuteNonQuery("dbo.USER_DeleteContext",
                                       transaction,
                                       CurrentDB.CreateStringInputParameter("@chvContext", DbType.AnsiString, context.Name, true));
             transaction.Commit();
         }
         contexts.Remove(context);
     }
 }
Esempio n. 4
0
        public override IList <IUser> GetUsersByDomain(string domain)
        {
            if (domain == null)
            {
                domain = string.Empty;
            }
            var dsUser = CurrentDB.ExecuteDataSet("dbo.USER_GetUsersByDomain",
                                                  CurrentDB.CreateStringInputParameter("@chvDomain",
                                                                                       DbType.AnsiString,
                                                                                       domain,
                                                                                       false));
            var uc = GetUsersFromDataSet(dsUser);

            UserCache.AddUsersToCache(uc);
            return(uc);
        }
Esempio n. 5
0
        protected virtual void SaveProperty(IUser user, IProperty property, ITransaction transaction)
        {
            var propertyValue = string.Empty;

            if (!property.IsEmpty())
            {
                propertyValue = property.ToSerializedString();
            }

            if (propertyValue.Length > 0)
            {
                CurrentDB.ExecuteNonQuery("dbo.USER_UpdateProperty",
                                          transaction,
                                          CurrentDB.CreateInputParameter("@guidUserId", DbType.Guid, new Guid(user.Id)),
                                          CurrentDB.CreateStringInputParameter("@chvProperty", DbType.AnsiString, property.Name),
                                          CurrentDB.CreateStringInputParameter("@chvnPropertyValue",
                                                                               DbType.String,
                                                                               propertyValue.Length <= 4000 ? propertyValue : null),
                                          CurrentDB.CreateStringInputParameter("@txtnExtPropertyValue",
                                                                               DbType.String,
                                                                               propertyValue.Length > 4000 ? propertyValue : null),
                                          CurrentDB.CreateStringInputParameter("@chvContext",
                                                                               DbType.AnsiString,
                                                                               property.Context == null ||
                                                                               string.IsNullOrEmpty(property.Context.Name)
                                                                                                       ? null
                                                                                                       : property.Context.Name));
            }
            else
            {
                CurrentDB.ExecuteNonQuery("dbo.USER_DeleteProperty",
                                          transaction,
                                          CurrentDB.CreateInputParameter("@guidUserId", DbType.Guid, new Guid(user.Id)),
                                          CurrentDB.CreateStringInputParameter("@chvPropertyName", DbType.AnsiString, property.Name),
                                          CurrentDB.CreateStringInputParameter("@chvContextName",
                                                                               DbType.AnsiString,
                                                                               property.Context == null ||
                                                                               string.IsNullOrEmpty(property.Context.Name)
                                                                                                       ? null
                                                                                                       : property.Context.Name));
            }

            property.IsDirty = false;
        }
Esempio n. 6
0
        public override IUser GetUserByUserName(string userName, string domain)
        {
            var user = UserCache.GetUserByUserName(userName, domain);

            if (user != null)
            {
                return(user);
            }
            var dsUser = CurrentDB.ExecuteDataSet("dbo.USER_GetByUsername",
                                                  CurrentDB.CreateStringInputParameter("@chvnUsername",
                                                                                       DbType.String,
                                                                                       userName),
                                                  CurrentDB.CreateStringInputParameter("@chvDomain",
                                                                                       DbType.AnsiString,
                                                                                       domain ?? string.Empty));

            user = GetUserFromDataSet(dsUser);
            UserCache.AddUserToCache(user);
            return(user);
        }
Esempio n. 7
0
        protected virtual IPropertyCollection GetPropertiesByUserId(string userId, IContext context)
        {
            IPropertyCollection upc = null;

            var dsUser = CurrentDB.ExecuteDataSet("dbo.USER_GetProperties",
                                                  CurrentDB.CreateInputParameter("@guidUserID", DbType.Guid, new Guid(userId)),
                                                  CurrentDB.CreateStringInputParameter("@chvContext",
                                                                                       DbType.AnsiString,
                                                                                       context == null ||
                                                                                       string.IsNullOrEmpty(context.Name)
                                                                                                               ? null
                                                                                                               : context.Name));

            // translate dataset
            if (dsUser.Tables.Count > 0)
            {
                dsUser.Tables[0].PrimaryKey = new[] { dsUser.Tables[0].Columns["PropertyName"] };
                var rows = dsUser.Tables[0].Rows;
                upc = GetPropertiesFromDataRows(rows, context, null);
            }
            return(upc);
        }
Esempio n. 8
0
        protected virtual void SaveUserInstance(IUser user, ITransaction transaction)
        {
            var dsUser = CurrentDB.ExecuteDataSet("dbo.USER_Update",
                                                  transaction,
                                                  CurrentDB.CreateInputParameter("@guidUserId",
                                                                                 DbType.Guid,
                                                                                 new Guid(user.Id)),
                                                  CurrentDB.CreateStringInputParameter("@chvnUsername",
                                                                                       DbType.String,
                                                                                       user.UserName,
                                                                                       false),
                                                  CurrentDB.CreateStringInputParameter("@chvDomain",
                                                                                       DbType.AnsiString,
                                                                                       user.Domain,
                                                                                       false));
            var status = (int)dsUser.Tables[0].Rows[0]["STATUS"];

            if (status == AddStatusUsernameTaken)
            {
                throw new UserNameAlreadyExistsException("Cannot save user. User name already exists.");
            }
        }
Esempio n. 9
0
        protected virtual ContextSchema GetContextSchema(string contextName, ITransaction transaction)
        {
            if (contextSchema.Keys.Contains(contextName))
            {
                return(contextSchema[contextName]);
            }

            lock (padlock) {
                if (contextSchema.Keys.Contains(contextName))
                {
                    return(contextSchema[contextName]);
                }

                var pdt = new List <PropertyDefinition>();

                IDataParameter[] prams =
                {
                    CurrentDB.CreateStringInputParameter("@chvContext", DbType.AnsiString, contextName, true)
                };

                DataSet dsUser;
                if (transaction == null)
                {
                    dsUser = CurrentDB.ExecuteDataSet("dbo.USER_GetPropertySchema", prams);
                }
                else
                {
                    dsUser = CurrentDB.ExecuteDataSet("dbo.USER_GetPropertySchema", transaction, prams);
                }

                // translate dataset
                if (dsUser.Tables.Count > 0)
                {
                    foreach (DataRow row in dsUser.Tables[0].Rows)
                    {
                        var propertyName = (string)row["PropertyName"];

                        Type propertyType;
                        if (!row.IsNull("AssemblyPath"))
                        {
                            var assembly = Assembly.LoadFrom((string)row["AssemblyPath"]);
                            propertyType = assembly.GetType((string)row["DataType"]);
                        }
                        else if (!row.IsNull("AssemblyName"))
                        {
                            var assembly = Assembly.Load((string)row["AssemblyName"]);
                            propertyType = assembly.GetType((string)row["DataType"]);
                        }
                        else
                        {
                            propertyType = Type.GetType((string)row["DataType"]);
                        }
                        if (propertyType == null)
                        {
                            throw new NotSupportedException(string.Format("The given property has a type {0} that can not be loaded.",
                                                                          row["DataType"]));
                        }
                        var pd = new PropertyDefinition(propertyName, propertyType);
                        pdt.Add(pd);
                    }
                }
                var schema = new ContextSchema(pdt);
                contextSchema.Add(contextName, schema);
                return(schema);
            }
        }