Changeset 1184:03073d3aa649


Ignore:
Timestamp:
09/15/06 13:29:18 (7 years ago)
Author:
boothby@…
Branch:
default
Message:

[project @ worksheet locking]

Location:
sage/server/notebook
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sage/server/notebook/css.py

    r1183 r1184  
    11271127  width: 100%; 
    11281128} 
     1129 
     1130span.red{ 
     1131  color:red; 
     1132} 
     1133 
    11291134 
    11301135 
  • sage/server/notebook/js.py

    r1183 r1184  
    7575var sub_introspecting = false; 
    7676 
    77 var worksheet_id=0;   // The current worksheet.  Where else does this get set? 
     77// Info about the current worksheet.  These get set in notebook.py 
     78var worksheet_id=0;    
     79var worksheet_filename=''; 
     80var worksheet_name=''; 
    7881 
    7982var id_to_delete=-1; 
     
    107110var in_slide_mode = false; //whether or not we're in slideshow mode 
    108111var slide_hidden = false; //whether the current slide has the hidden input class 
     112 
     113var worksheet_locked; 
    109114 
    110115/////////////////////////////////////////////////////////////////// 
     
    488493/////////////////////////////////////////////////////////////////// 
    489494 
    490 function add_worksheet(name) { 
     495function add_worksheet(name,pass) { 
    491496    async_request('async_obj_add_worksheet', '/add_worksheet', 
    492                    add_worksheet_callback, 'name='+name) 
    493 } 
    494  
    495 function add_worksheet_callback(status, response_text) { 
     497                   add_worksheet_callback, 'name='+name+'&passcode='+pass) 
     498} 
     499 
     500function add_worksheet_callback(status,response_text) { 
    496501    if (status == "success") { 
    497502        /* expect response_text to encode a pair consisting of 
     
    606611    var add_worksheet_box = get_element('new_worksheet_box'); 
    607612    name = add_worksheet_box.value; 
     613    var add_worksheet_pass = get_element('new_worksheet_pass'); 
     614    pass = add_worksheet_pass.value; 
    608615    add_worksheet_box.value = '';     
    609     add_worksheet(name); 
     616    add_worksheet_pass.value = '';     
     617    add_worksheet(name,pass); 
    610618} 
    611619 
     
    630638} 
    631639 
     640function unlock_worksheet() { 
     641    lock = get_element("worksheet_lock"); 
     642    lock.innerHTML = 'Enter Passcode: <input onKeyPress="return unlock_worksheet_submit(event,value);" id="lock_input">'; 
     643    lock.innerHTML+= '<span id="unlock_error" class="red"></span>'; 
     644    lock_input = get_element("lock_input"); 
     645    lock_input.focus();     
     646} 
     647 
     648function unlock_worksheet_submit(e,passcode) { 
     649    if(is_submit(e)) { 
     650        document.cookie = "ws_"+worksheet_name+"_passcode="+passcode; 
     651        async_request('async_unlock', '/unlock_worksheet', unlock_worksheet_callback, 'worksheet_id='+worksheet_id); 
     652        return false; 
     653    } 
     654    return true; 
     655} 
     656 
     657function unlock_worksheet_callback(status, response_text) { 
     658    if(status == 'success' && response_text == 'ok') { 
     659        lock = get_element("worksheet_lock"); 
     660        lock.parentNode.removeChild(lock); 
     661        worksheet_locked = false; 
     662    } else { 
     663        lock_input = get_element("lock_input"); 
     664        lock_input.value = ""; 
     665        lock_input.focus(); 
     666        txt = get_element('unlock_error'); 
     667        if(txt) 
     668            txt.innerHTML = 'incorrect'; 
     669    } 
     670} 
    632671 
    633672/////////////////////////////////////////////////////////////////// 
     
    10251064 
    10261065function evaluate_cell(id, action) { 
     1066    if(worksheet_locked) { 
     1067        alert("This worksheet is locked.  Click on the word [locked] next to the worksheet name to unlock it.") 
     1068        return; 
     1069    } 
     1070 
    10271071    active_cell_list = active_cell_list.concat([id]); 
    10281072 
  • sage/server/notebook/notebook.py

    r1183 r1184  
    561561        return set([W.id() for W in self.__worksheets.itervalues()]) 
    562562 
    563     def create_new_worksheet(self, name='untitled'): 
     563    def create_new_worksheet(self, name='untitled', passcode=''): 
    564564        if name in self.__worksheets.keys(): 
    565565            raise KeyError, 'name (=%s) already taken.'%name 
    566566        name = str(name) 
     567        passcode = str(passcode) 
    567568        wids = self.worksheet_ids() 
    568569        id = 0 
     
    573574            raise ValueError, 'there can be at most %s worksheets'%MAX_WORKSHEETS 
    574575        self.__next_worksheet_id += 1 
    575         W = worksheet.Worksheet(name, self, id, system=self.system()) 
     576        W = worksheet.Worksheet(name, self, id, system=self.system(), passcode=passcode) 
    576577        self.__worksheets[name] = W 
    577578        return W 
     
    718719        return head 
    719720 
    720     def _html_body(self, worksheet_id, show_debug=False): 
     721    def _html_body(self, worksheet_id, show_debug=False, worksheet_authorized=False): 
    721722        worksheet = self.get_worksheet_with_id(worksheet_id) 
    722723        if worksheet.computing(): 
     
    727728        add_new_worksheet_menu = """ 
    728729             <div class="add_new_worksheet_menu" id="add_worksheet_menu"> 
    729              <input id="new_worksheet_box" class="add_new_worksheet_menu" 
     730             Name: <input id="new_worksheet_box" class="add_new_worksheet_menu" 
    730731                    onKeyPress="if(is_submit(event)) process_new_worksheet_menu_submit();"></input> 
     732             Pass: <input id="new_worksheet_pass" class="add_new_worksheet_menu" 
     733                    onKeyPress="if(is_submit(event)) process_new_worksheet_menu_submit();"></input> 
     734 
    731735             <button class="add_new_worksheet_menu"  onClick="process_new_worksheet_menu_submit();">add</button> 
    732              &nbsp;&nbsp;&nbsp;<span class="X" onClick="hide_add_new_worksheet_menu()">X</span> 
     736             <span class="X" onClick="hide_add_new_worksheet_menu()">X</span> 
    733737             </div> 
    734738        """              
     
    790794            body += " onFocus='debug_focus();' onBlur='debug_blur();'></textarea>" 
    791795            body += "</div>" 
    792         body += worksheet.html() 
     796        body += worksheet.html(authorized = worksheet_authorized) + '\n</div>\n' 
    793797 
    794798        # The blank space given by '<br>'*15  is needed so the input doesn't get 
     
    818822        body += '<script language=javascript>focus(%s)</script>\n'%(worksheet[0].id()) 
    819823        body += '<script language=javascript>jsmath_init();</script>\n' 
     824 
     825        if worksheet_authorized: 
     826            body += '<script language=javascript>worksheet_locked=false;</script>' 
     827        else: 
     828            body += '<script language=javascript>worksheet_locked=true;</script>' 
    820829 
    821830        if worksheet.computing(): 
     
    953962         """%(css.css(self.color()),js.javascript()) 
    954963 
    955     def html(self, worksheet_id=None, authorized=False, show_debug=False): 
     964    def html(self, worksheet_id=None, authorized=False, show_debug=False, worksheet_authorized=False): 
    956965        if worksheet_id is None: 
    957966            W = self.default_worksheet() 
     
    965974 
    966975        if authorized: 
    967             body = self._html_body(worksheet_id,show_debug=show_debug) 
     976            body = self._html_body(worksheet_id,show_debug=show_debug, worksheet_authorized=worksheet_authorized) 
    968977        else: 
    969978            body = self._html_authorize() 
    970979 
    971         body += '<script language=javascript>worksheet_id=%s; worksheet_filename="%s"</script>'%(worksheet_id, W.filename()) 
     980        body += '<script language=javascript>worksheet_id=%s; worksheet_filename="%s"; worksheet_name="%s";</script>'%(worksheet_id, W.filename(), W.name()) 
    972981 
    973982        head = self._html_head(worksheet_id) 
  • sage/server/notebook/server.py

    r1097 r1184  
    6161        typ = C['type'][0] 
    6262        W = notebook.get_worksheet_that_has_cell_with_id(id) 
    63         cell = W.get_cell_with_id(id) 
    64         cell.set_cell_output_type(typ) 
     63        if self.auth_worksheet(W): 
     64            cell = W.get_cell_with_id(id) 
     65            cell.set_cell_output_type(typ) 
    6566 
    6667    def hide_all(self): 
     
    6869        id = int(C['worksheet_id'][0]) 
    6970        W = notebook.get_worksheet_with_id(id) 
    70         W.hide_all() 
     71        if self.auth_worksheet(W): 
     72            W.hide_all() 
    7173 
    7274    def restart_sage(self): 
     
    7476        id = int(C['worksheet_id'][0]) 
    7577        W = notebook.get_worksheet_with_id(id) 
    76         W.restart_sage() 
    77         self.wfile.write('done') 
     78        if self.auth_worksheet(W): 
     79            W.restart_sage() 
     80            self.wfile.write('done') 
    7881 
    7982    def show_all(self): 
    8083        C = self.get_postvars() 
    8184        id = int(C['worksheet_id'][0]) 
    82         W = notebook.get_worksheet_with_id(id) 
    83         W.show_all() 
     85        if self.auth_worksheet(W): 
     86            W = notebook.get_worksheet_with_id(id) 
     87            W.show_all() 
    8488 
    8589    def eval_cell(self, newcell=False, introspect=False): 
     
    9094        verbose('%s: %s'%(id, input_text)) 
    9195        W = notebook.get_worksheet_that_has_cell_with_id(id) 
     96        if not self.auth_worksheet(W): 
     97            return 
     98 
    9299        cell = W.get_cell_with_id(id) 
    93100        if not introspect: 
     
    118125 
    119126        W = notebook.get_worksheet_that_has_cell_with_id(id) 
     127        if not self.auth_worksheet(W): 
     128            return 
     129 
    120130        cell = W.get_cell_with_id(id) 
    121131        #TB: this tends to obliterate long cells -- if the user doesn't submit between 
     
    135145        verbose("Adding new cell before cell with id %s"%id) 
    136146        W = notebook.get_worksheet_that_has_cell_with_id(id) 
     147        if not self.auth_worksheet(W): 
     148            return 
     149 
    137150        cell = W.new_cell_before(id) 
    138151        #notebook.save() 
     
    145158        verbose("Adding new cell after cell with id %s"%id) 
    146159        W = notebook.get_worksheet_that_has_cell_with_id(id) 
     160        if not self.auth_worksheet(W): 
     161            return 
     162 
    147163        cell = W.new_cell_after(id) 
    148164        #notebook.save() 
     
    155171        verbose("Deleting cell with id %s"%id) 
    156172        W = notebook.get_worksheet_that_has_cell_with_id(id) 
     173        if not self.auth_worksheet(W): 
     174            return 
     175 
    157176        if len(W) <= 1 or W.is_last_id_and_previous_is_nonempty(id): 
    158177            self.wfile.write('ignore') 
     
    219238        C = self.get_postvars() 
    220239        worksheet_id = int(C['worksheet_id'][0]) 
    221         worksheet = notebook.get_worksheet_with_id(worksheet_id) 
    222         t = worksheet.interrupt() 
     240        W = notebook.get_worksheet_with_id(worksheet_id) 
     241        if not self.auth_worksheet(W): 
     242            return 
     243 
     244        t = W.interrupt() 
    223245        if t: 
    224246            self.wfile.write('ok') 
     
    229251        C = self.get_postvars() 
    230252        worksheet_name = C['name'][0] 
    231         try: 
    232             W = notebook.create_new_worksheet(worksheet_name) 
     253        passcode = C['passcode'][0] 
     254        try: 
     255            W = notebook.create_new_worksheet(worksheet_name, passcode) 
    233256        except ValueError, msg: 
    234257            print msg 
     
    238261        self.wfile.write(new_worksheet_list + SEP + str(W.name())) 
    239262 
     263    def auth_worksheet(self, W): 
     264        n = W.name() 
     265        passcode = '' 
     266        if self.cookie.has_key('ws_%s_passcode'%n): 
     267            passcode = self.cookie['ws_%s_passcode'%n].value 
     268        return W.auth(passcode) 
     269 
     270    def unlock_worksheet(self): 
     271        C = self.get_postvars() 
     272        worksheet_id = int(C['worksheet_id'][0]) 
     273        W = notebook.get_worksheet_with_id(worksheet_id) 
     274        if not self.auth_worksheet(W): 
     275            self.wfile.write('failed') 
     276        else: 
     277            self.wfile.write('ok') 
     278 
    240279    def delete_worksheet(self): 
    241280        C = self.get_postvars() 
    242281        worksheet_name = C['name'][0] 
     282        W = notebook.get_worksheet_with_name(worksheet_name) 
     283        if not self.auth_worksheet(W): 
     284            return 
     285 
    243286        try: 
    244287            W = notebook.delete_worksheet(worksheet_name) 
     
    450493    def show_page(self, worksheet_id=None,show_debug=False): 
    451494        self.send_head() 
     495        W = notebook.get_worksheet_with_id(worksheet_id) 
    452496        self.wfile.write(notebook.html(worksheet_id=worksheet_id, 
    453497                                       authorized=self.authorize(), 
    454                                        show_debug=show_debug)) 
     498                                       show_debug=show_debug, 
     499                                       worksheet_authorized = self.auth_worksheet(W))) 
    455500 
    456501 
     
    463508    def do_GET(self): 
    464509        verbose("GET: " + self.path) 
     510        self.get_cookie() 
    465511        # The question mark hack here is so that images will be reloaded when 
    466512        # the async request requests the output text for a computation. 
     
    499545            self.file_not_found() 
    500546 
    501     def authorize(self): 
     547    def get_cookie(self): 
    502548        self.cookie=Cookie.SimpleCookie() 
    503549        if self.headers.has_key('cookie'): 
    504550            self.cookie=Cookie.SimpleCookie(self.headers.getheader("cookie")) 
     551 
     552    def authorize(self): 
    505553        username = password = ""; 
    506554        if self.cookie.has_key('username'): 
     
    511559 
    512560    def do_POST(self): 
     561        self.get_cookie() 
    513562        content_type, post_dict = cgi.parse_header(self.headers.getheader('content-type')) 
    514563        verbose("POST: %s"%post_dict) 
     
    567616            elif self.path[-17:] == '/delete_worksheet': 
    568617                self.delete_worksheet() 
     618            elif self.path[-17:] == '/unlock_worksheet': 
     619                self.unlock_worksheet() 
    569620#            elif self.path == '/upload_worksheet': 
    570621#                self.upload_worksheet_local_file() 
  • sage/server/notebook/worksheet.py

    r1178 r1184  
    4343 
    4444class Worksheet: 
    45     def __init__(self, name, notebook, id, system=None): 
     45    def __init__(self, name, notebook, id, system=None, passcode = ''): 
    4646        name = ' '.join(name.split()) 
    4747        self.__id = id 
     
    5050        self.__name = name 
    5151        self.__notebook = notebook 
     52        self.__passcode = passcode 
    5253        dir = list(name) 
    5354        for i in range(len(dir)): 
     
    8182            for C in self.__cells: 
    8283                C.set_worksheet(self) 
     84 
     85    def passcode(self): 
     86        try: 
     87            return self.__passcode 
     88        except AttributeError: 
     89            self.__passcode = '' 
     90            return '' 
    8391 
    8492    def filename(self): 
     
    537545        return self.__notebook.format_completions_as_html(id, rows) 
    538546 
    539  
    540  
    541  
     547    def auth(self, passcode): 
     548        if self.passcode() == '': 
     549            return True 
     550        else: 
     551            return self.passcode() == passcode 
    542552 
    543553    def _strip_synchro_from_start_of_output(self, s): 
     
    10511061        return s 
    10521062 
    1053     def html(self, include_title=True, do_print=False): 
     1063    def html(self, include_title=True, do_print=False, authorized=False): 
    10541064        n = len(self.__cells) 
    10551065        s = '' 
     
    10611071            else: 
    10621072                system ='' 
    1063             s += '<div class="worksheet_title">Worksheet: %s%s</div>\n'%(self.name(),system) 
     1073            if not authorized: 
     1074                lock_text = '&nbsp;&nbsp;<span id="worksheet_lock" class="locked" onClick="unlock_worksheet()">[locked]</span>' 
     1075            else: 
     1076                lock_text = '' 
     1077 
     1078            s += '<div class="worksheet_title">Worksheet: %s%s%s</div>\n'%(self.name(),system,lock_text) 
    10641079        D = self.__notebook.defaults() 
    10651080        ncols = D['word_wrap_cols'] 
Note: See TracChangeset for help on using the changeset viewer.