Exemplo n.º 1
0
        public static void BuildPassword(MSSQLDB db, SqlTransaction trans, Int64 context, Int64 entityId, Int64 enterpriseId)
        {
            String pwdMethod = "random";
            String pwdValue  = "";

            using (DataTable dtRules = db.Select("select password_rule from context c where c.id = " + context + " and (c.password_rule is not null and rtrim(LTRIM(c.password_rule)) <> '')", trans))
            {
                if ((dtRules != null) && (dtRules.Rows.Count > 0))
                {
                    String v = dtRules.Rows[0]["password_rule"].ToString().Trim();

                    if (v.IndexOf("[") != -1)
                    {
                        Regex rex = new Regex(@"(.*?)\[(.*?)\]");
                        Match m   = rex.Match(v);
                        if (m.Success)
                        {
                            pwdMethod = m.Groups[1].Value.ToLower();
                            pwdValue  = m.Groups[2].Value;
                        }
                    }
                    else
                    {
                        pwdMethod = v;
                    }
                }
            }

            switch (pwdMethod)
            {
            case "default":
                //Nada a senha ja foi definida
                break;

            case "field":
                throw new NotImplementedException();

                /*
                 * Int64 fieldId = 0;
                 * Int64.TryParse(pwdValue, out fieldId);
                 * using (DataTable dtFields = db.Select("select * from identity_field where identity_id = " + this.IdentityId + " and field_id = " + fieldId, trans))
                 *  if ((dtFields != null) && (dtFields.Rows.Count > 0))
                 *  {
                 *      pwdValue = dtFields.Rows[0]["value"].ToString();
                 *  }*/
                break;

            default:     //Random
                pwdValue = "";
                break;
            }

            //Se a senha continua vazia, gera uma randômica
            if ((pwdValue == null) || (pwdValue == ""))
            {
                pwdValue = RandomPassword.Generate(14, 16);
            }

            Boolean MustChangePassword = true;

            String pwd = "";

            using (EnterpriseKeyConfig sk = new EnterpriseKeyConfig(db.conn, enterpriseId, trans))

                using (CryptApi cApi = new CryptApi(sk.ServerCert, Encoding.UTF8.GetBytes(pwdValue)))
                    pwd = Convert.ToBase64String(cApi.ToBytes());


            String sql = "update entity set password = @password, change_password = getdate(), must_change_password = @must where id = @entityId";

            SqlParameterCollection par = GetSqlParameterObject();

            par.Add("@entityId", SqlDbType.BigInt).Value = entityId;

            par.Add("@password", SqlDbType.VarChar, pwd.Length).Value = pwd;
            par.Add("@must", SqlDbType.Bit).Value = MustChangePassword;

            db.AddUserLog(LogKey.User_PasswordChanged, null, "Engine", UserLogLevel.Info, 0, 0, context, 0, 0, entityId, 0, "Password changed", "", trans);

            db.ExecuteNonQuery(sql, CommandType.Text, par, trans);
        }