ajax.py 7.82 KB
Newer Older
1
2
3
# -*- coding: utf-8 -*-

import cherrypy
Marcel Rieger's avatar
Marcel Rieger committed
4
from vispa.controller import AbstractController
Marcel's avatar
Marcel committed
5
from vispa.controller.filesystem import FSAjaxController
6
from vispa.models.user import User
7
from vispa.models.workspace import Workspace, WorkspaceState
8
from vispa.models.preference import VispaPreference, ExtensionPreference
9
from vispa.models.shortcuts import VispaShortcuts, ExtensionShortcuts
10
from vispa import AjaxException
11
import vispa.workspace
Gero Müller's avatar
Gero Müller committed
12
import logging
13
import json
14
import os
15
import paramiko
16
import json as JSON
17

Gero Müller's avatar
Gero Müller committed
18
logger = logging.getLogger(__name__)
19

Gero Müller's avatar
Gero Müller committed
20

21
class AjaxController(AbstractController):
22

23
    def __init__(self, root):
24
25
        super(AjaxController, self).__init__(mount_static=False)

26
        self._root = root
27
        self.fs    = FSAjaxController()
28
29

    @cherrypy.expose
Gero Müller's avatar
Gero Müller committed
30
    @cherrypy.tools.user(on=False)
31
    @cherrypy.tools.method(accept="POST")
32
    def login(self, username, password):
33
34
35
36
37
38
        user = User.login(cherrypy.request.db, username, password)

        cherrypy.session["user_id"]   = user.id
        cherrypy.session["user_name"] = user.name

        return { "userId": user.id, "sessionId": cherrypy.session.id }
39
40

    @cherrypy.expose
Gero Müller's avatar
Gero Müller committed
41
    @cherrypy.tools.user(on=False)
42
    @cherrypy.tools.method(accept="POST")
43
    def register(self, username, email):
44
45
46
47
48
49
        user = User.register(cherrypy.request.db, username, email)

        if vispa.config("web", "registration.autoactive", True):
            return { "hash": user.hash }

        elif vispa.config("web", "registration.sendmail", False):
50
            User.send_registration_mail(user.name, user.email, user.hash)
51
52

        return { "hash": None }
53
54

    @cherrypy.expose
Gero Müller's avatar
Gero Müller committed
55
    @cherrypy.tools.user(on=False)
56
    @cherrypy.tools.method(accept="POST")
57
    def forgotpassword(self, username):
58
        if vispa.config("web", "forgot.use", False) and not cherrypy.session.get("is_guest", False):
59
60
            User.forgot_password(cherrypy.request.db, username)
        else:
61
            raise AjaxException("Password restoring not allowed!")
62
63

    @cherrypy.expose
Gero Müller's avatar
Gero Müller committed
64
    @cherrypy.tools.user(on=False)
65
    @cherrypy.tools.method(accept="POST")
66
    def setpassword(self, hash, password):
67
68
        user = User.set_password(cherrypy.request.db, hash, password)
        if user.active():
69
70
            cherrypy.session["user_id"] = unicode(user.id)
            cherrypy.session["user_name"] = user.name
Marcel's avatar
Marcel committed
71

72
    @cherrypy.expose
73
    @cherrypy.tools.method(accept="POST")
74
    def setvispapreference(self, name, value=u"{}"):
75
        db = cherrypy.request.db
76
        user_id = self.get("user_id")
77
        # change the preference section
78
        VispaPreference.set_value(db, user_id, name, value)
79
80

    @cherrypy.expose
81
    @cherrypy.tools.method(accept="POST")
82
    def setextensionpreference(self, name, value=u"{}"):
83
        db = cherrypy.request.db
84
        user_id = self.get("user_id")
85
        # change the preference section
86
        ExtensionPreference.set_value(db, user_id, name, value)
Marcel's avatar
Marcel committed
87

88
    @cherrypy.expose
89
    @cherrypy.tools.method(accept="POST")
90
    def setvispashortcuts(self, name, value=u"{}"):
91
        db = cherrypy.request.db
92
93
        user_id = self.get("user_id")
        VispaShortcuts.set_value(db, user_id, name, value)
94
95

    @cherrypy.expose
96
    @cherrypy.tools.method(accept="POST")
97
    def setextensionshortcuts(self, name, value=u"{}"):
98
        db = cherrypy.request.db
99
100
        user_id = self.get("user_id")
        ExtensionShortcuts.set_value(db, user_id, name, value)
101

102
    @cherrypy.expose
103
    @cherrypy.tools.method(accept="POST")
Marcel Rieger's avatar
Marcel Rieger committed
104
    def addworkspace(self, name, host, login, key=None, cmd=None):
Marcel Rieger's avatar
Marcel Rieger committed
105
        if not vispa.config("workspace", "add", True):
106
            raise AjaxException("No permission to add a new Workspace!")
Marcel Rieger's avatar
Marcel Rieger committed
107

108
109
        db = self.get("db")
        user_id = self.get("user_id")
110

111
112
113
114
115
        # does the user already have a workspace with that name?
        workspaces = Workspace.get_user_workspaces(db, user_id)
        for workspace in workspaces:
            # case insensitive
            if name.lower() == workspace.name.lower():
116
                raise AjaxException("Workspace '%s' already in use!" % name)
117

118
        # add the workspace
Marcel Rieger's avatar
Marcel Rieger committed
119
120
        workspace = Workspace.add(db, user_id, name, host, login, key=key,
                                  command=cmd)
121
        return self._root.workspace_data(workspace)
122
123

    @cherrypy.expose
124
    @cherrypy.tools.method(accept="POST")
125
    def deleteworkspace(self, wid):
Marcel Rieger's avatar
Marcel Rieger committed
126
        if not vispa.config("workspace", "add", True):
127
            raise AjaxException("No permission to delete a Workspace!")
Marcel Rieger's avatar
Marcel Rieger committed
128

129
        db = cherrypy.request.db
Marcel Rieger's avatar
Marcel Rieger committed
130

131
        # can the user edit the workspace?
132
133
        workspace = Workspace.get_by_id(db, wid)
        if not isinstance(workspace, Workspace):
134
            raise AjaxException("Unknown Workspace!")
135
        if not workspace.can_edit(self.get("user_id")):
136
            raise AjaxException("No permission to delete this Workspace!")
137

138
139
140
        # remove it
        success = Workspace.remove(db, wid)
        if not success:
141
            raise AjaxException("Couldn't remove this Workspace!")
142

143
    @cherrypy.expose
144
    @cherrypy.tools.method(accept="POST")
145
146
147
    def connectworkspace(self, wid, password=None):
        db       = cherrypy.request.db
        user     = cherrypy.request.user
148
        password = password or cherrypy.session.get("guest_password", None)
Gero Müller's avatar
Gero Müller committed
149

150
151
        # is workspace owned by the user?
        workspace = Workspace.get_by_id(db, wid)
152
        if not isinstance(workspace, Workspace) or not workspace.has_access(user):
153
            raise AjaxException("Unknown Workspace! (id: %s)" % wid)
Gero Müller's avatar
Gero Müller committed
154

155
        vispa.workspace.connect(workspace, user, db, password)
156
157

    @cherrypy.expose
158
    @cherrypy.tools.method(accept="POST")
159
    def disconnectworkspace(self, wid):
160
161
162
        db   = cherrypy.request.db
        user = cherrypy.request.user

163
164
        # is workspace owned by the user?
        workspace = Workspace.get_by_id(db, wid)
165
        if not isinstance(workspace, Workspace) or not workspace.has_access(user):
166
            raise AjaxException("Unknown Workspace! (id: %s)" % wid)
167

Gero Müller's avatar
Gero Müller committed
168
169
170
        # disconnect
        vispa.workspace.disconnect(workspace)

171
    @cherrypy.expose
172
    @cherrypy.tools.method(accept="GET")
173
174
175
176
177
178
179
    def getworkspacedata(self, wid=None):
        workspace = None
        if wid:
            db = cherrypy.request.db
            workspace = Workspace.get_by_id(db, wid)

        return self._root.workspace_data(workspace=workspace)
Marcel's avatar
Marcel committed
180

181
    @cherrypy.expose
182
    @cherrypy.tools.method(accept="GET")
183
184
185
186
187
188
189
    def getworkspacestate(self, wid):
        db     = cherrypy.request.db
        userid = cherrypy.request.user.id

        return WorkspaceState.get_state(db, wid, userid) or "{}"

    @cherrypy.expose
190
    @cherrypy.tools.method(accept="POST")
191
192
193
194
195
196
    def setworkspacestate(self, wid, state):
        db     = cherrypy.request.db
        userid = cherrypy.request.user.id

        WorkspaceState.update_state(db, wid, userid, state)

Gero Müller's avatar
Gero Müller committed
197
198
199
200
    @cherrypy.expose
    def localuser(self):
        local = cherrypy.request.local
        remote = cherrypy.request.remote
201
202
203
204
205
206
        socket_uid = vispa.Netstat.get_socket_owner(
            local.ip or local.name,
            local.port,
            remote.ip or remote.name,
            remote.port
        )
Gero Müller's avatar
Gero Müller committed
207
208
        logger.info("Socket uid: %s" % socket_uid)
        server_uid = os.getuid()
Marcel's avatar
Marcel committed
209
        logger.info("Server uid: %s" % server_uid)
210

Marcel Rieger's avatar
Marcel Rieger committed
211
    @cherrypy.expose
212
    @cherrypy.tools.method(accept="POST")
Marcel Rieger's avatar
Marcel Rieger committed
213
    def feedback(self, content, anonymous):
Marcel Rieger's avatar
Marcel Rieger committed
214
215
216
        feedback_address = vispa.config("web", "feedback.address", "")
        if feedback_address == "":
            return
217
        subject = vispa.config("web", "feedback.subject", "VISPA Feedback")
Marcel Rieger's avatar
Marcel Rieger committed
218
219
220
221
222
223
224
        if self.convert(anonymous, bool):
            prefix = "Anonymous feedback:\n\n"
        else:
            uname = cherrypy.request.user.name
            umail = cherrypy.request.user.email
            prefix = "Feedback from user '%s' (%s):\n\n" % (uname, umail)
        vispa.send_mail(feedback_address, subject, prefix + content)