public ActionResult DeleteSnapshot()
        {
            DGSnapshot snapshot = DGSnapshot.ByID(Convert.ToInt64(Request["ID"]));

            if (snapshot != null)
            {
                if (snapshot.Diagram.UserID == User.ID)
                {
                    snapshot.Delete();
                    return(Content("OK"));
                }
                else
                {
                    return(Content("ERROR:Permission Denied"));
                }
            }
            else
            {
                return(Content("ERROR:Bad Input ID"));
            }
        }
Beispiel #2
0
        private void HandleRequest(string command)
        {
            switch (command)
            {
            case "PreserveSession":
            {
                Respond(User != null ? "true" : "false");
            }
            break;

            case "GetCurrentUser":
            {
                Respond(User);
            }
            break;

            case "GetDiagrams":
            {
                List <DGDiagram> dgs = DGDiagram.ByUserID(UserID);
                Respond(dgs);
            }
            break;

            case "GetSharedDiagrams":
            {
                if (UserID != 0)
                {
                    Respond(DGVSharedDiagram.ByEmail(User.Name));
                }
                else
                {
                    Respond("ERROR:Not Logged In");
                }
            }
            break;

            case "GetSharing":
            {
                if (UserID != 0)
                {
                    Int64 diagramID = Convert.ToInt64(_Ctx.Request["DiagramID"]);

                    DGDiagram dg = DGDiagram.ByID(diagramID);
                    if (dg != null && dg.UserID == UserID)
                    {
                        Respond(DGShare.ByDiagram(diagramID));
                    }
                }
            }
            break;

            case "ShareDiagram":
            {
                if (UserID != 0)
                {
                    string email     = _Ctx.Request["Email"];
                    Int64  diagramID = Convert.ToInt64(_Ctx.Request["DiagramID"]);

                    DGDiagram dg = DGDiagram.ByID(diagramID);
                    if (dg.UserID == UserID)
                    {
                        dg.Share(email, DGSharePermisson.View);
                        Respond(DGShare.ByDiagram(diagramID));
                    }
                    else
                    {
                        Respond("ERROR:Permission Denied");
                    }
                }
            }
            break;

            case "UnshareDiagram":
            {
                if (UserID != 0)
                {
                    string email     = _Ctx.Request["Email"];
                    Int64  diagramID = Convert.ToInt64(_Ctx.Request["DiagramID"]);

                    DGDiagram dg = DGDiagram.ByID(diagramID);
                    if (dg.UserID == UserID)
                    {
                        dg.Unshare(email);
                        Respond(DGShare.ByDiagram(diagramID));
                    }
                }
            }
            break;

            case "SaveDiagram":
            {
                string def = _Ctx.Request["Diagram"];

                JavaScriptSerializer ser = new JavaScriptSerializer();
                JSDiagram            dg  = ser.Deserialize <JSDiagram>(def);

                DGDiagram diagram = DGDiagram.ByUserID(UserID).ByID(dg.ID);
                if (diagram == null)
                {
                    diagram        = new DGDiagram();
                    diagram.UserID = UserID;
                }

                if (diagram.UserID != UserID)
                {
                    Respond("ERROR:Permission Denied");
                }
                else
                {
                    diagram.Name = dg.Name;
                    diagram.PrimarySnapshot.DiagramData = def;
                    diagram.Save();

                    Respond(diagram.ID);
                }
            }
            break;

            case "TogglePublicAccess":
            {
                DGDiagram dg = DGDiagram.ByID(Convert.ToInt64(_Ctx.Request["ID"]));
                if (dg != null)
                {
                    if (dg.UserID == UserID)
                    {
                        dg.AllowPublicAccess = !dg.AllowPublicAccess;
                        dg.Save();
                        Respond(dg.AllowPublicAccess);
                    }
                    else
                    {
                        Respond("ERROR:Permission Denied");
                    }
                }
            }
            break;

            case "GetDiagram":
            {
                if (_Ctx.Request["ID"] == "WelcomeDiagram")
                {
                    string email = ConfigurationManager.AppSettings["WelcomeDiagram Email"];
                    DGUser user  = DGUser.ByName(email);
                    if (user != null)
                    {
                        DGDiagram dg = DGDiagram.ByUserID(user.ID).ByName(ConfigurationManager.AppSettings["WelcomeDiagram Name"]);
                        Respond(dg);
                    }
                }
                else
                {
                    DGDiagram dg = DGDiagram.ByID(Convert.ToInt64(_Ctx.Request["ID"]));
                    if (dg != null)
                    {
                        if (dg.UserID == UserID ||
                            dg.AllowPublicAccess ||
                            DGShare.ByEmailAndDiagram(User.Name, dg.ID).Count > 0)
                        {
                            Respond(dg);
                        }
                    }
                }
            }
            break;

            case "DeleteDiagram":
            {
                DGDiagram dg = DGDiagram.ByUserID(UserID).ByID(Convert.ToInt64(_Ctx.Request["ID"]));
                dg.Delete();
                Respond("OK");
            }
            break;

            case "AddSnapshot":
            {
                string def = _Ctx.Request["Snapshot"];

                JavaScriptSerializer ser = new JavaScriptSerializer();
                JSDiagram            dg  = ser.Deserialize <JSDiagram>(def);

                // TODO: check user
                DGDiagram diagram = DGDiagram.ByUserID(UserID).ByID(Convert.ToInt64(_Ctx.Request["DGID"]));
                if (diagram != null)
                {
                    DGSnapshot snapshot = new DGSnapshot()
                    {
                        Name        = _Ctx.Request["Name"],
                        DiagramID   = diagram.ID,
                        IsDefault   = false,
                        DiagramData = def
                    };
                    snapshot.Save();

                    Respond(diagram.ID);
                }
            }
            break;

            case "GetSnapshots":
            {
                DGDiagram diagram = DGDiagram.ByID(Convert.ToInt64(_Ctx.Request["DGID"])) ?? new DGDiagram();

                if (diagram.UserID == UserID ||
                    DGShare.ByEmailAndDiagram(User.Name, diagram.ID).Count > 0)
                {
                    Respond(DGSnapshot.ByDiagramID(diagram.ID));
                }
            }
            break;

            case "DeleteSnapshot":
            {
                DGSnapshot snapshot = DGSnapshot.ByID(Convert.ToInt64(_Ctx.Request["ID"]));
                if (snapshot != null)
                {
                    if (snapshot.Diagram.UserID == User.ID)
                    {
                        snapshot.Delete();
                        Respond("OK");
                    }
                    else
                    {
                        Respond("ERROR:Permission Denied");
                    }
                }
                else
                {
                    Respond("ERROR:Bad Input ID");
                }
            }
            break;

            case "LoadSnapshot":
            {
                DGSnapshot snapshot = DGSnapshot.ByID(Convert.ToInt64(_Ctx.Request["ID"]));
                if (snapshot.Diagram.UserID == UserID ||
                    DGShare.ByEmailAndDiagram(User.Name, snapshot.Diagram.ID).Count > 0)
                {
                    Respond(snapshot);
                }
                else
                {
                    Respond("ERROR:Permission Denied");
                }
            }
            break;

            case "CreateScript":
            {
                string def = _Ctx.Request["Diagram"];

                JavaScriptSerializer ser = new JavaScriptSerializer();
                JSDiagram            dg  = ser.Deserialize <JSDiagram>(def);

                dg.Relate();
                SQAdapter adp = (_Ctx.Request["Lang"] == "MySQL") ? (SQAdapter) new MySQLAdapter()
                            : (_Ctx.Request["Lang"] == "SQL Server") ? (SQAdapter) new SQLServerAdapter()
                            : (_Ctx.Request["Lang"] == "PostgreSQL") ? (SQAdapter) new PostgreSQLAdapter()
                            : (SQAdapter) new SQLiteAdapter();

                StringBuilder sb = new StringBuilder();

                foreach (JSTable table in dg.Tables)
                {
                    SQTable t = table.GetTable();

                    sb.AppendLine("-- Create Table: " + t.Name);
                    sb.AppendLine("--------------------------------------------------------------------------------");
                    sb.Append(adp.WriteCreateTable(t));
                    sb.AppendLine("");
                    sb.AppendLine("");
                    sb.AppendLine("");
                }

                foreach (JSFKey fk in dg.FKeys)
                {
                    sb.AppendLine(string.Format("-- Create Foreign Key: {0}.{1} -> {2}.{3}", fk.From.GetTable().Name, fk.From.GetColumn().Name, fk.To.GetTable().Name, fk.To.GetColumn().Name));
                    sb.Append(adp.WriteAddForeignKey(fk.From.GetColumn().GetColumn(), fk.To.GetColumn().GetColumn()));
                    sb.AppendLine("");
                    sb.AppendLine("");
                    sb.AppendLine("");
                }

                Respond(sb.ToString());
            }
            break;

            case "DoChangeScript":
            {
            }
            break;
            }
        }