Example #1
0
        /// <summary>
        /// Updates an existing resource.
        /// </summary>
        /// <param name="parameters">The update parameters</param>
        /// <returns>The updated resource</returns>
        public virtual PSResource UpdatePSResource(UpdatePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            ResourceGetResult getResource;

            try
            {
                getResource = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName,
                                                                     resourceIdentity);
            }
            catch (CloudException)
            {
                throw new ArgumentException(ProjectResources.ResourceDoesntExists);
            }

            string newProperty = SerializeHashtable(parameters.PropertyObject,
                                                    addValueLayer: false);

            Dictionary <string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(parameters.Tag, validate: true);

            ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName, resourceIdentity,
                                                              new BasicResource
            {
                Location   = getResource.Resource.Location,
                Properties = newProperty,
                Tags       = tagDictionary
            });

            ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);

            return(getResult.Resource.ToPSResource(this, false));
        }
Example #2
0
        /// <summary>
        /// Updates an existing resource.
        /// </summary>
        /// <param name="parameters">The update parameters</param>
        /// <returns>The updated resource</returns>
        public virtual PSResource UpdatePSResource(UpdatePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            ResourceGetResult getResource;

            try
            {
                getResource = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName,
                                                                     resourceIdentity);
            }
            catch (CloudException)
            {
                throw new ArgumentException(ProjectResources.ResourceDoesntExists);
            }

            string newProperty = SerializeHashtable(parameters.PropertyObject,
                                                    addValueLayer: false);

            ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName, resourceIdentity,
                                                              new ResourceCreateOrUpdateParameters
            {
                Resource = new BasicResource
                {
                    Location   = getResource.Resource.Location,
                    Properties = newProperty
                }
            });

            ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);

            return(getResult.Resource.ToPSResource(this));
        }
        public void Given_InvalidHttpStatusCode_GetInsightsResourceAsync_ShouldThrow_Exception(string appInsightsName, string resourceGroup, HttpStatusCode statusCode)
        {
            this._appInsights.SetupGet(p => p.Name).Returns(appInsightsName);
            this._appInsights.SetupGet(p => p.ResourceGroup).Returns(resourceGroup);

            this._settings.SetupGet(p => p.ApplicationInsight).Returns(this._appInsights.Object);

            var resourceResult = new ResourceGetResult()
            {
                StatusCode = statusCode
            };

            this._resourceOperations.Setup(p => p.GetAsync(It.IsAny <string>(), It.IsAny <ResourceIdentity>(), It.IsAny <CancellationToken>())).ReturnsAsync(resourceResult);

            this._resourceClient.SetupGet(p => p.Resources).Returns(this._resourceOperations.Object);

            Func <Task> func = async() => { var result = await this._service.GetInsightsResourceAsync(this._resourceClient.Object).ConfigureAwait(false); };

            func.ShouldThrow <HttpResponseException>().And.Response.StatusCode.Should().Be(statusCode);
        }
        public async void Given_Parameter_GetInsightsResourceAsync_ShouldReturn_Result(string appInsightsName, string resourceGroup, string location)
        {
            this._appInsights.SetupGet(p => p.Name).Returns(appInsightsName);
            this._appInsights.SetupGet(p => p.ResourceGroup).Returns(resourceGroup);

            this._settings.SetupGet(p => p.ApplicationInsight).Returns(this._appInsights.Object);

            var resource       = new GenericResourceExtended(location);
            var resourceResult = new ResourceGetResult()
            {
                StatusCode = HttpStatusCode.OK, Resource = resource
            };

            this._resourceOperations.Setup(p => p.GetAsync(It.IsAny <string>(), It.IsAny <ResourceIdentity>(), It.IsAny <CancellationToken>())).ReturnsAsync(resourceResult);

            this._resourceClient.SetupGet(p => p.Resources).Returns(this._resourceOperations.Object);

            var result = await this._service.GetInsightsResourceAsync(this._resourceClient.Object).ConfigureAwait(false);

            result.Location.Should().BeEquivalentTo(location);
        }
Example #5
0
        /// <summary>
        /// Creates a new resource.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created resource</returns>
        public virtual PSResource CreatePSResource(CreatePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            if (ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
            {
                WriteVerbose(string.Format("Resource group \"{0}\" is found.", parameters.ResourceGroupName));
            }
            else
            {
                parameters.ConfirmAction(parameters.Force,
                                         ProjectResources.ResourceGroupDoesntExistsAdd,
                                         ProjectResources.AddingResourceGroup,
                                         parameters.Name,
                                         () => CreateOrUpdateResourceGroup(parameters.ResourceGroupName, parameters.Location, null));

                if (!ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
                {
                    throw new ArgumentException(ProjectResources.ResourceGroupDoesntExists);
                }
                else
                {
                    WriteVerbose(string.Format("Created resource group '{0}' in location '{1}'", parameters.Name, parameters.Location));
                }
            }

            bool resourceExists = ResourceManagementClient.Resources.CheckExistence(parameters.ResourceGroupName, resourceIdentity).Exists;

            Action createOrUpdateResource = () =>
            {
                WriteVerbose(string.Format("Creating resource \"{0}\" started.", parameters.Name));

                Dictionary <string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(parameters.Tag, validate: true);

                ResourceCreateOrUpdateResult createOrUpdateResult = ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName,
                                                                                                                      resourceIdentity,
                                                                                                                      new BasicResource
                {
                    Location   = parameters.Location,
                    Properties = SerializeHashtable(parameters.PropertyObject, addValueLayer: false),
                    Tags       = tagDictionary
                });

                if (createOrUpdateResult.Resource != null)
                {
                    WriteVerbose(string.Format("Creating resource \"{0}\" complete.", parameters.Name));
                }
            };

            if (resourceExists && !parameters.Force)
            {
                parameters.ConfirmAction(parameters.Force,
                                         ProjectResources.ResourceAlreadyExists,
                                         ProjectResources.NewResourceMessage,
                                         parameters.Name,
                                         createOrUpdateResource);
            }
            else
            {
                createOrUpdateResource();
            }

            ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);

            return(getResult.Resource.ToPSResource(this, false));
        }
Example #6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod != "POST")
            {
                return;
            }

            String area = "";

            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["area"]))
            {
                area = (String)RouteData.Values["area"];
            }

            Int64 enterpriseId = 0;

            if ((Session["enterprise_data"]) != null && (Session["enterprise_data"] is EnterpriseData))
            {
                enterpriseId = ((EnterpriseData)Session["enterprise_data"]).Id;
            }

            Boolean newItem = false;

            if ((RouteData.Values["new"] != null) && (RouteData.Values["new"] == "1"))
            {
                newItem = true;
            }

            String ApplicationVirtualPath = Session["ApplicationVirtualPath"].ToString();

            LMenu menu1 = new LMenu("Dashboard", ApplicationVirtualPath + "admin/");
            LMenu menu2 = new LMenu("Recursos", ApplicationVirtualPath + "admin/resource/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : ""));
            LMenu menu3 = new LMenu("Recursos", ApplicationVirtualPath + "admin/resource/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : ""));

            WebJsonResponse contentRet = null;

            String html  = "";
            String eHtml = "";
            String js    = null;

            String errorTemplate = "<span class=\"empty-results\">{0}</span>";

            //Verifica se está sendo selecionada uma role
            Int64 resourceId = 0;

            try
            {
                resourceId = Int64.Parse((String)RouteData.Values["id"]);

                if (resourceId < 0)
                {
                    resourceId = 0;
                }
            }
            catch { }

            String            error       = "";
            ResourceGetResult retResource = null;
            String            filter      = "";
            HashData          hashData    = new HashData(this);


            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["filter"]))
            {
                filter = (String)RouteData.Values["filter"];
            }

            if ((resourceId > 0) && (area.ToLower() != "search"))
            {
                try
                {
                    var tmpReq = new
                    {
                        jsonrpc    = "1.0",
                        method     = "resource.get",
                        parameters = new
                        {
                            resourceid = resourceId
                        },
                        id = 1
                    };

                    String rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                    String jData = "";
                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                        jData = WebPageAPI.ExecuteLocal(database, this, rData);


                    if (String.IsNullOrWhiteSpace(jData))
                    {
                        throw new Exception("");
                    }

                    retResource = JSON.Deserialize <ResourceGetResult>(jData);
                    if (retResource == null)
                    {
                        error = MessageResource.GetMessage("resource_not_found");
                        //ret = new WebJsonResponse("", MessageResource.GetMessage("user_not_found"), 3000, true);
                    }
                    else if (retResource.error != null)
                    {
                        error       = retResource.error.data;
                        retResource = null;
                    }
                    else if (retResource.result == null || retResource.result.info == null)
                    {
                        error       = MessageResource.GetMessage("resource_not_found");
                        retResource = null;
                    }
                    else
                    {
                        menu3.Name = retResource.result.info.name;
                    }
                }
                catch (Exception ex)
                {
                    error = MessageResource.GetMessage("api_error");
                    Tools.Tool.notifyException(ex, this);
                    retResource = null;
                    //ret = new WebJsonResponse("", MessageResource.GetMessage("api_error"), 3000, true);
                }
            }

            switch (area)
            {
            case "":
            case "search":
            case "content":
                if (newItem)
                {
                    try
                    {
                        var tmpReq = new
                        {
                            jsonrpc    = "1.0",
                            method     = "context.list",
                            parameters = new { },
                            id         = 1
                        };

                        error = "";
                        String rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                        String jData = "";
                        using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                            jData = WebPageAPI.ExecuteLocal(database, this, rData);


                        if (String.IsNullOrWhiteSpace(jData))
                        {
                            throw new Exception("");
                        }

                        ContextListResult contextList = JSON.Deserialize <ContextListResult>(jData);
                        if (contextList == null)
                        {
                            error = MessageResource.GetMessage("context_not_found");
                            //ret = new WebJsonResponse("", MessageResource.GetMessage("user_not_found"), 3000, true);
                        }
                        else if (contextList.error != null)
                        {
                            error = contextList.error.data;
                        }
                        else if (contextList.result == null)
                        {
                            error = MessageResource.GetMessage("context_not_found");
                        }
                        else
                        {
                            var tmpReq2 = new
                            {
                                jsonrpc    = "1.0",
                                method     = "proxy.list",
                                parameters = new { },
                                id         = 1
                            };

                            error = "";
                            rData = SafeTrend.Json.JSON.Serialize2(tmpReq2);
                            jData = "";
                            using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                jData = WebPageAPI.ExecuteLocal(database, this, rData);


                            if (String.IsNullOrWhiteSpace(jData))
                            {
                                throw new Exception("");
                            }

                            ProxyListResult proxyList = JSON.Deserialize <ProxyListResult>(jData);
                            if (proxyList == null)
                            {
                                error = MessageResource.GetMessage("proxy_not_found");
                                //ret = new WebJsonResponse("", MessageResource.GetMessage("user_not_found"), 3000, true);
                            }
                            else if (proxyList.error != null)
                            {
                                error = proxyList.error.data;
                            }
                            else if (proxyList.result == null)
                            {
                                error = MessageResource.GetMessage("proxy_not_found");
                            }
                            else
                            {
                                html  = "<h3>Adição de recurso</h3>";
                                html += "<form id=\"form_add_resource\" method=\"post\" action=\"" + ApplicationVirtualPath + "admin/resource/action/add_resource/\"><div class=\"no-tabs pb10\">";
                                html += "<div class=\"form-group\"><label>Nome</label><input id=\"resource_name\" name=\"resource_name\" placeholder=\"Digite o nome do recurso\" type=\"text\"\"></div>";
                                html += "<div class=\"form-group\"><label>Contexto</label><select id=\"resource_context\" name=\"resource_context\" ><option value=\"\"></option>";
                                foreach (ContextData c in contextList.result)
                                {
                                    html += "<option value=\"" + c.context_id + "\" " + (hashData.GetValue("context") == c.context_id.ToString() ? "selected" : "") + ">" + c.name + "</option>";
                                }
                                html += "</select></div>";
                                html += "<div class=\"form-group\"><label>Proxy</label><select id=\"resource_proxy\" name=\"resource_proxy\" ><option value=\"\"></option>";
                                foreach (ProxyData p in proxyList.result)
                                {
                                    html += "<option value=\"" + p.proxy_id + "\" " + (hashData.GetValue("proxy") == p.proxy_id.ToString() ? "selected" : "") + ">" + p.name + "</option>";
                                }
                                html += "</select></div>";
                                html += "<div class=\"clear-block\"></div></div>";
                                html += "<button type=\"submit\" id=\"user-profile-password-save\" class=\"button secondary floatleft\">Adicionar</button>    <a href=\"" + ApplicationVirtualPath + "admin/resource/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" class=\"button link floatleft\">Cancelar</a></form>";

                                contentRet = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        error = MessageResource.GetMessage("api_error");
                    }

                    if (error != "")
                    {
                        contentRet = new WebJsonResponse("#content-wrapper", String.Format(errorTemplate, error));
                    }
                }
                else
                {
                    if (retResource == null)
                    {
                        Int32   page     = 1;
                        Int32   pageSize = 20;
                        Boolean hasNext  = true;

                        Int32.TryParse(Request.Form["page"], out page);

                        if (page < 1)
                        {
                            page = 1;
                        }


                        String roleTemplate = "<div id=\"role-list-{0}\" data-id=\"{0}\" data-name=\"{1}\" data-total=\"{2}\" class=\"app-list-item\">";
                        roleTemplate += "<table>";
                        roleTemplate += "   <tbody>";
                        roleTemplate += "       <tr>";
                        roleTemplate += "           <td class=\"col1\">";
                        roleTemplate += "               <span id=\"total_{0}\" class=\"total \">{2}</span>";
                        roleTemplate += "               <a href=\"" + ApplicationVirtualPath + "admin/resource_plugin/#resource/{0}\">";
                        roleTemplate += "                   <div class=\"app-btn a-btn\"><span class=\"a-btn-inner\">Ver vínculos</span></div>";
                        roleTemplate += "               </a>";
                        roleTemplate += "           </td>";
                        roleTemplate += "           <td class=\"col2\">";
                        roleTemplate += "               <div class=\"title\"><span class=\"name field-editor\" id=\"resource_name_{0}\" data-id=\"{0}\" data-function=\"iamadmin.editTextField('#resource_name_{0}',null,resourceNameEdit);\">{1}</span><span class=\"date\">{3}</span><div class=\"clear-block\"></div></div>";
                        roleTemplate += "               <div class=\"description\">";
                        roleTemplate += "                   <div class=\"first\">{4}</div>";
                        roleTemplate += "               </div>";
                        roleTemplate += "               <div class=\"links\">";
                        roleTemplate += "                   <div class=\"\"><a href=\"" + ApplicationVirtualPath + "admin/resource/{0}/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\"><div class=\"ico icon-change\">Editar</div></a><a href=\"" + ApplicationVirtualPath + "admin/roles/{0}/action/delete_all_users/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" class=\"confirm-action\" confirm-title=\"Exclusão\" confirm-text=\"Deseja excluir definitivamente todos os usuários do perfil '{1}'?\" ok=\"Excluir\" cancel=\"Cancelar\"><div class=\"ico icon-close\">Excluir usuários</div></a></div>";
                        roleTemplate += "                   <div class=\"last\">{5}</div>";
                        roleTemplate += "               </div>";
                        roleTemplate += "           </td>";
                        roleTemplate += "       </tr>";
                        roleTemplate += "   </tbody>";
                        roleTemplate += "</table></div>";

                        js += "resourceNameEdit = function(thisId, changedText) { iamadmin.changeName(thisId,changedText); };";

                        html += "<div id=\"box-container\" class=\"box-container\">";

                        String query = "";
                        try
                        {
                            String rData = "";

                            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["query"]))
                            {
                                query = (String)RouteData.Values["query"];
                            }

                            if (String.IsNullOrWhiteSpace(query) && !String.IsNullOrWhiteSpace(hashData.GetValue("query")))
                            {
                                query = hashData.GetValue("query");
                            }

                            if (String.IsNullOrWhiteSpace(query))
                            {
                                var tmpReq = new
                                {
                                    jsonrpc    = "1.0",
                                    method     = "resource.list",
                                    parameters = new
                                    {
                                        page_size = pageSize,
                                        page      = page,
                                        filter    = new { contextid = hashData.GetValue("context"), proxyid = hashData.GetValue("proxy") }
                                    },
                                    id = 1
                                };

                                rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                            }
                            else
                            {
                                var tmpReq = new
                                {
                                    jsonrpc    = "1.0",
                                    method     = "resource.search",
                                    parameters = new
                                    {
                                        text      = query,
                                        page_size = pageSize,
                                        page      = page,
                                        filter    = new { contextid = hashData.GetValue("context"), proxyid = hashData.GetValue("proxy") }
                                    },
                                    id = 1
                                };

                                rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                            }

                            String jData = "";
                            using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                jData = WebPageAPI.ExecuteLocal(database, this, rData);


                            if (String.IsNullOrWhiteSpace(jData))
                            {
                                throw new Exception("");
                            }

                            ResourceListResult ret2 = JSON.Deserialize <ResourceListResult>(jData);
                            if (ret2 == null)
                            {
                                eHtml  += String.Format(errorTemplate, MessageResource.GetMessage("resource_not_found"));
                                hasNext = false;
                            }
                            else if (ret2.error != null)
                            {
#if DEBUG
                                eHtml += String.Format(errorTemplate, ret2.error.data + ret2.error.debug);
#else
                                eHtml += String.Format(errorTemplate, ret2.error.data);
#endif
                                hasNext = false;
                            }
                            else if (ret2.result == null || (ret2.result.Count == 0 && page == 1))
                            {
                                eHtml  += String.Format(errorTemplate, MessageResource.GetMessage("resource_not_found"));
                                hasNext = false;
                            }
                            else
                            {
                                foreach (ResourceData resource in ret2.result)
                                {
                                    String text = "Contexto: " + resource.context_name + ", Proxy: " + resource.proxy_name;

                                    html += String.Format(roleTemplate, resource.resource_id, resource.name, resource.resource_plugin_qty, (resource.create_date > 0 ? "Criado em " + MessageResource.FormatDate(new DateTime(1970, 1, 1).AddSeconds(resource.create_date), true) : ""), text, (resource.resource_plugin_qty > 0 ? "" : "<a class=\"confirm-action\" href=\"" + ApplicationVirtualPath + "admin/resource/" + resource.resource_id + "/action/delete/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" confirm-title=\"Exclusão\" confirm-text=\"Deseja excluir definitivamente o recurso '" + resource.name + "'?\" ok=\"Excluir\" cancel=\"Cancelar\"><div class=\"ico icon-close\">Apagar</div></a>"));
                                }

                                if (ret2.result.Count < pageSize)
                                {
                                    hasNext = false;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            eHtml += String.Format(errorTemplate, MessageResource.GetMessage("api_error"));
                        }

                        if (page == 1)
                        {
                            html += "</div>";

                            html += "<span class=\"empty-results content-loading role-list-loader hide\"></span>";

                            contentRet = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                        }
                        else
                        {
                            contentRet = new WebJsonResponse("#content-wrapper #box-container", (eHtml != "" ? eHtml : html), true);
                        }

                        contentRet.js = js + "$( document ).unbind('end_of_scroll');";

                        if (hasNext)
                        {
                            contentRet.js += "$( document ).bind( 'end_of_scroll.loader_role', function() { $( document ).unbind('end_of_scroll.loader_role'); $('.role-list-loader').removeClass('hide'); iamadmin.getPageContent2( { page: " + ++page + ", search:'" + (!String.IsNullOrWhiteSpace(query) ? query : "") + "' }, function(){ $('.role-list-loader').addClass('hide'); } ); });";
                        }
                    }
                    else    //Esta sendo selecionado a role
                    {
                        if (error != "")
                        {
                            contentRet = new WebJsonResponse("#content-wrapper", String.Format(errorTemplate, error));
                        }
                        else
                        {
                            switch (filter)
                            {
                            case "":
                                String infoTemplate = "<div class=\"form-group\">";
                                infoTemplate += "<label>{0}</label>";
                                infoTemplate += "<span class=\"no-edit\">{1}</span></div>";
                                String jsAdd = "";

                                if (hashData.GetValue("edit") == "1")
                                {
                                    var tmpReq = new
                                    {
                                        jsonrpc    = "1.0",
                                        method     = "context.list",
                                        parameters = new { },
                                        id         = 1
                                    };

                                    error = "";
                                    String rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                                    String jData = "";
                                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                        jData = WebPageAPI.ExecuteLocal(database, this, rData);


                                    if (String.IsNullOrWhiteSpace(jData))
                                    {
                                        throw new Exception("");
                                    }

                                    ContextListResult contextList = JSON.Deserialize <ContextListResult>(jData);
                                    if (contextList == null)
                                    {
                                        error = MessageResource.GetMessage("context_not_found");
                                        //ret = new WebJsonResponse("", MessageResource.GetMessage("user_not_found"), 3000, true);
                                    }
                                    else if (contextList.error != null)
                                    {
                                        error = contextList.error.data;
                                    }
                                    else if (contextList.result == null)
                                    {
                                        error = MessageResource.GetMessage("context_not_found");
                                    }
                                    else
                                    {
                                        var tmpReq2 = new
                                        {
                                            jsonrpc    = "1.0",
                                            method     = "proxy.list",
                                            parameters = new { },
                                            id         = 1
                                        };

                                        error = "";
                                        rData = SafeTrend.Json.JSON.Serialize2(tmpReq2);
                                        jData = "";
                                        using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                            jData = WebPageAPI.ExecuteLocal(database, this, rData);


                                        if (String.IsNullOrWhiteSpace(jData))
                                        {
                                            throw new Exception("");
                                        }

                                        ProxyListResult proxyList = JSON.Deserialize <ProxyListResult>(jData);
                                        if (contextList == null)
                                        {
                                            error = MessageResource.GetMessage("proxy_not_found");
                                            //ret = new WebJsonResponse("", MessageResource.GetMessage("user_not_found"), 3000, true);
                                        }
                                        else if (contextList.error != null)
                                        {
                                            error = contextList.error.data;
                                        }
                                        else if (contextList.result == null)
                                        {
                                            error = MessageResource.GetMessage("proxy_not_found");
                                        }
                                        else
                                        {
                                            html += "<form  id=\"form_resource_change\"  method=\"POST\" action=\"" + ApplicationVirtualPath + "admin/resource/" + retResource.result.info.resource_id + "/action/change/\">";
                                            html += "<h3>Edição de recurso</h3>";
                                            html += "<div class=\"no-tabs pb10\">";

                                            String contexto = "";
                                            contexto += "<select id=\"resource_context\" name=\"resource_context\" ><option value=\"\"></option>";
                                            foreach (ContextData c in contextList.result)
                                            {
                                                contexto += "<option value=\"" + c.context_id + "\" " + (retResource.result.info.context_id == c.context_id ? "selected" : "") + ">" + c.name + "</option>";
                                            }
                                            contexto += "</select>";

                                            String proxy = "";
                                            proxy += "<select id=\"resource_proxy\" name=\"resource_proxy\" ><option value=\"\"></option>";
                                            foreach (ProxyData c in proxyList.result)
                                            {
                                                proxy += "<option value=\"" + c.proxy_id + "\" " + (retResource.result.info.proxy_id == c.proxy_id ? "selected" : "") + ">" + c.name + "</option>";
                                            }
                                            proxy += "</select>";

                                            html += String.Format(infoTemplate, "Nome", "<input id=\"name\" name=\"name\" placeholder=\"Digite o nome do recurso\" type=\"text\"\" value=\"" + retResource.result.info.name + "\">");
                                            html += String.Format(infoTemplate, "Contexto", contexto);
                                            html += String.Format(infoTemplate, "Proxy", proxy);
                                            html += String.Format(infoTemplate, "Desabilitado", "<input id=\"disabled\" name=\"disabled\" type=\"checkbox\" " + (!retResource.result.info.enabled ? "checked" : "") + ">");
                                            html += String.Format(infoTemplate, "Criado em", MessageResource.FormatDate(((DateTime) new DateTime(1970, 1, 1)).AddSeconds(retResource.result.info.create_date), false));

                                            html += "<div class=\"clear-block\"></div></div>";
                                        }
                                    }
                                }
                                else
                                {
                                    html += "<h3>Informações gerais<div class=\"btn-box\"><div class=\"a-btn ico icon-change\" onclick=\"iamadmin.changeHash( 'edit/1' );\">Editar</div></div></h3>";
                                    html += "<div class=\"no-tabs pb10\">";

                                    html += String.Format(infoTemplate, "Nome", retResource.result.info.name);
                                    html += String.Format(infoTemplate, "Contexto", retResource.result.info.context_name);
                                    html += String.Format(infoTemplate, "Proxy", retResource.result.info.proxy_name);
                                    html += String.Format(infoTemplate, "Desabilitado", (!retResource.result.info.enabled ? MessageResource.GetMessage("yes") : MessageResource.GetMessage("no")));
                                    html += String.Format(infoTemplate, "Criado em", MessageResource.FormatDate(((DateTime) new DateTime(1970, 1, 1)).AddSeconds(retResource.result.info.create_date), false));

                                    html += "<div class=\"clear-block\"></div></div>";
                                }

                                if (hashData.GetValue("edit") == "1")
                                {
                                    html += "<button type=\"submit\" id=\"resource-save\" class=\"button secondary floatleft\">Salvar</button>    <a class=\"button link floatleft\" onclick=\"iamadmin.changeHash( 'edit/0' );\">Cancelar</a></form>";
                                }

                                contentRet    = new WebJsonResponse("#content-wrapper", html);
                                contentRet.js = jsAdd;
                                break;

                            case "identity":
                                break;
                            }
                        }
                    }
                }

                break;

            case "sidebar":
                if (menu1 != null)
                {
                    html += "<div class=\"sep\"><div class=\"section-nav-header\">";
                    html += "    <div class=\"crumbs\">";
                    html += "        <div class=\"subject subject-color\">";
                    html += "            <a href=\"" + menu1.HRef + "\">" + menu1.Name + "</a>";
                    html += "        </div>";
                    if (menu2 != null)
                    {
                        html += "        <div class=\"topic topic-color\">";
                        html += "            <a href=\"" + menu2.HRef + "\">" + menu2.Name + "</a>";
                        html += "        </div>";
                    }
                    html += "    </div>";
                    if (menu3 != null)
                    {
                        html += "    <div class=\"crumbs tutorial-title\">";
                        html += "        <h2 class=\"title tutorial-color\">" + menu3.Name + "</h2>";
                        html += "    </div>";
                    }
                    html += "</div></div>";
                }

                if (!newItem)
                {
                    html += "<div class=\"sep\"><button class=\"a-btn-big a-btn\" type=\"button\" onclick=\"window.location='" + ApplicationVirtualPath + "admin/resource/new/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "'\">Novo recurso</button></div>";

                    switch (filter)
                    {
                    case "add_user":
                        break;

                    default:
                        /*if (retRole != null)
                         *  html += "<div class=\"sep\"><button class=\"a-btn-big a-btn\" type=\"button\" onclick=\"window.location='" + ApplicationVirtualPath + "admin/roles/" + retRole.result.info.role_id + "/add_user/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "'\">Adicionar usuários</button></div>";*/
                        break;
                    }
                }

                contentRet = new WebJsonResponse("#main aside", html);
                break;

            case "mobilebar":
                break;


            case "buttonbox":

                switch (filter)
                {
                case "":
                    if (retResource == null)
                    {
                        js = "";
                        try
                        {
                            String rData = SafeTrend.Json.JSON.Serialize2(new
                            {
                                jsonrpc    = "1.0",
                                method     = "context.list",
                                parameters = new
                                {
                                    page_size = Int32.MaxValue
                                },
                                id = 1
                            });
                            String jData = "";
                            using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                jData = WebPageAPI.ExecuteLocal(database, this, rData);


                            ContextListResult contextList = JSON.Deserialize <ContextListResult>(jData);
                            if ((contextList != null) && (contextList.error == null) && (contextList.result != null))
                            {
                                html += "<select id=\"filter_context\" name=\"filter_context\" ><option value=\"\">Todos os contextos</option>";
                                foreach (ContextData c in contextList.result)
                                {
                                    html += "<option value=\"context/" + c.context_id + "\" " + (hashData.GetValue("context") == c.context_id.ToString() ? "selected" : "") + ">" + c.name + "</option>";
                                }
                                html += "</select>";
                                js   += "$('#filter_context').change(function() { iamadmin.changeHash( $( this ).val() ); });";
                            }
                        }
                        catch (Exception ex) { }

                        try
                        {
                            String rData = SafeTrend.Json.JSON.Serialize2(new
                            {
                                jsonrpc    = "1.0",
                                method     = "proxy.list",
                                parameters = new
                                {
                                    page_size = Int32.MaxValue
                                },
                                id = 1
                            });
                            String jData = "";
                            using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                jData = WebPageAPI.ExecuteLocal(database, this, rData);


                            ProxyListResult proxyList = JSON.Deserialize <ProxyListResult>(jData);
                            if ((proxyList != null) && (proxyList.error == null) && (proxyList.result != null))
                            {
                                html += "<select id=\"filter_proxy\" name=\"filter_proxy\" ><option value=\"\">Todos os proxies</option>";
                                foreach (ProxyData p in proxyList.result)
                                {
                                    html += "<option value=\"proxy/" + p.proxy_id + "\" " + (hashData.GetValue("proxy") == p.proxy_id.ToString() ? "selected" : "") + ">" + p.name + "</option>";
                                }
                                html += "</select>";

                                js += "$('#filter_proxy').change(function() { iamadmin.changeHash( $( this ).val() ); });";
                            }
                        }
                        catch (Exception ex) { }

                        contentRet    = new WebJsonResponse("#btnbox", html);
                        contentRet.js = js;
                    }
                    break;
                }
                break;
            }

            if (contentRet != null)
            {
                if (!String.IsNullOrWhiteSpace((String)Request["cid"]))
                {
                    contentRet.callId = (String)Request["cid"];
                }

                Retorno.Controls.Add(new LiteralControl(contentRet.ToJSON()));
            }
        }
Example #7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            WebJsonResponse contentRet = null;


            String action = "";

            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["action"]))
            {
                action = (String)RouteData.Values["action"];
            }

            Int64 resourceId = 0;

            if (action != "add_resource")
            {
                try
                {
                    resourceId = Int64.Parse((String)RouteData.Values["id"]);

                    if (resourceId < 0)
                    {
                        resourceId = 0;
                    }
                }
                catch { }

                if (resourceId == 0)
                {
                    contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    action     = "";
                }
            }

            String rData = "";
            //SqlConnection //conn = DB.GetConnection();
            String jData = "";

            try
            {
                switch (action)
                {
                case "delete":

                    var reqDel = new
                    {
                        jsonrpc    = "1.0",
                        method     = "resource.delete",
                        parameters = new
                        {
                            resourceid = resourceId
                        },
                        id = 1
                    };

                    rData = JSON.Serialize2(reqDel);

                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString())) jData = WebPageAPI.ExecuteLocal(database, this, rData);

                    if (String.IsNullOrWhiteSpace(jData))
                    {
                        throw new Exception("");
                    }

                    ResourceDeleteResult retDel = JSON.Deserialize <ResourceDeleteResult>(jData);
                    if (retDel == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else if (retDel.error != null)
                    {
                        contentRet = new WebJsonResponse("", retDel.error.data, 3000, true);
                    }
                    else if (!retDel.result)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else
                    {
                        contentRet = new WebJsonResponse();
                    }
                    break;

                case "change_name":

                    String name = Request.Form["name"];
                    if (String.IsNullOrEmpty(name))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("invalid_name"), 3000, true);
                        break;
                    }

                    var reqD = new
                    {
                        jsonrpc    = "1.0",
                        method     = "resource.change",
                        parameters = new
                        {
                            resourceid = resourceId,
                            name       = name
                        },
                        id = 1
                    };

                    rData = JSON.Serialize2(reqD);

                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString())) jData = WebPageAPI.ExecuteLocal(database, this, rData);

                    if (String.IsNullOrWhiteSpace(jData))
                    {
                        throw new Exception("");
                    }

                    ResourceGetResult retD = JSON.Deserialize <ResourceGetResult>(jData);
                    if (retD == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else if (retD.error != null)
                    {
                        contentRet = new WebJsonResponse("", retD.error.data, 3000, true);
                    }
                    else if (retD.result == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else if (retD.result == null || retD.result.info == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else
                    {
                        contentRet = new WebJsonResponse("#resource_name_" + resourceId, retD.result.info.name);
                    }
                    break;

                case "change":

                    String name1 = Request.Form["name"];
                    if (String.IsNullOrEmpty(name1))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("invalid_name"), 3000, true);
                        break;
                    }


                    String context_id1 = Request.Form["resource_context"];
                    if (String.IsNullOrEmpty(context_id1))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("select_context"), 3000, true);
                        break;
                    }

                    String proxy_id1 = Request.Form["resource_proxy"];
                    if (String.IsNullOrEmpty(proxy_id1))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("select_proxy"), 3000, true);
                        break;
                    }


                    var reqC = new
                    {
                        jsonrpc    = "1.0",
                        method     = "resource.change",
                        parameters = new
                        {
                            resourceid = resourceId,
                            name       = name1,
                            contextid  = context_id1,
                            proxyid    = proxy_id1,
                            enabled    = (!String.IsNullOrEmpty(Request.Form["disabled"]) ? false : true)
                        },
                        id = 1
                    };

                    rData = JSON.Serialize2(reqC);

                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString())) jData = WebPageAPI.ExecuteLocal(database, this, rData);

                    if (String.IsNullOrWhiteSpace(jData))
                    {
                        throw new Exception("");
                    }

                    ResourceGetResult retC = JSON.Deserialize <ResourceGetResult>(jData);
                    if (retC == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else if (retC.error != null)
                    {
                        contentRet = new WebJsonResponse("", retC.error.data, 3000, true);
                    }
                    else if (retC.result == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else if (retC.result == null || retC.result.info == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else
                    {
                        contentRet = new WebJsonResponse(Session["ApplicationVirtualPath"] + "admin/resource/" + resourceId + "/");
                    }
                    break;


                case "add_resource":
                    String resourceName = Request.Form["resource_name"];
                    if (String.IsNullOrEmpty(resourceName))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("type_resource_name"), 3000, true);
                        break;
                    }

                    String context_id = Request.Form["resource_context"];
                    if (String.IsNullOrEmpty(context_id))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("select_context"), 3000, true);
                        break;
                    }

                    String proxy_id = Request.Form["resource_proxy"];
                    if (String.IsNullOrEmpty(proxy_id))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("select_proxy"), 3000, true);
                        break;
                    }

                    var reqAddR = new
                    {
                        jsonrpc    = "1.0",
                        method     = "resource.new",
                        parameters = new
                        {
                            name      = resourceName,
                            contextid = context_id,
                            proxyid   = proxy_id
                        },
                        id = 1
                    };

                    rData = JSON.Serialize2(reqAddR);

                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString())) jData = WebPageAPI.ExecuteLocal(database, this, rData);

                    if (String.IsNullOrWhiteSpace(jData))
                    {
                        throw new Exception("");
                    }

                    ResourceGetResult retAddR = JSON.Deserialize <ResourceGetResult>(jData);
                    if (retAddR == null)
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else if (retAddR.error != null)
                    {
                        contentRet = new WebJsonResponse("", retAddR.error.data, 3000, true);
                        //Tools.Tool.notifyException(new Exception(retAdd.error.data + retAdd.error.debug), this);
                    }
                    else if ((retAddR.result == null) || (retAddR.result.info == null))
                    {
                        contentRet = new WebJsonResponse("", MessageResource.GetMessage("resource_not_found"), 3000, true);
                    }
                    else
                    {
                        contentRet = new WebJsonResponse(Session["ApplicationVirtualPath"] + "admin/resource/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : ""));
                    }

                    //

                    break;
                }
            }
            catch (Exception ex)
            {
                contentRet = new WebJsonResponse("", MessageResource.GetMessage("api_error"), 3000, true);
            }
            finally
            {
            }

            if (contentRet != null)
            {
                if (!String.IsNullOrWhiteSpace((String)Request["cid"]))
                {
                    contentRet.callId = (String)Request["cid"];
                }


                Retorno.Controls.Add(new LiteralControl(contentRet.ToJSON()));
            }
        }