1###############################################################################2#3# CoCalc: Collaborative Calculation in the Cloud4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################212223############################################################24# Account Settings25############################################################2627{webapp_client} = require('./webapp_client')28{alert_message} = require('./alerts')29account_page = require('./account_page')3031misc = require("misc")32{redux} = require('./smc-react')3334################################################35# Account creation36################################################3738first_login = true3940# load more of the app now that user is logged in.41load_app = (cb) ->42require.ensure [], ->43require('./r_account.cjsx') # initialize react-related account page44require('./projects.cjsx') # initialize project listing45require('./file_use.cjsx') # initialize file_use notifications46cb()4748signed_in = (mesg) ->49{analytics_event} = require('./misc_page')50analytics_event('account', 'signed_in') # user signed in51# the has_remember_me cookie is for usability: After a sign in we "mark" this client as being "known"52# next time the main landing page is visited, haproxy or hub will redirect to the client53# note: similar code is in redux_account.coffee → AccountActions::sign_out54{APP_BASE_URL} = require('./misc_page')55exp = misc.server_days_ago(-30).toGMTString()56document.cookie = "#{APP_BASE_URL}has_remember_me=true; expires=#{exp} ;path=/"57# Record which hub we're connected to.58redux.getActions('account').setState(hub: mesg.hub)59load_file = window.smc_target and window.smc_target != 'login'60if first_login61first_login = false62if not load_file63load_app ->64require('./history').load_target('projects')6566if load_file67# wait until account settings get loaded, then show target page68# HACK: This is hackish!, and will all go away with a more global use of React (and routing).69# The underscore below should make it clear that this is hackish.70redux.getTable('account')._table.once 'connected', ->71load_app ->72require('./history').load_target(window.smc_target)73window.smc_target = ''747576# Listen for pushed sign_in events from the server. This is one way that77# the sign_in function above can be activated, but not the only way.78webapp_client.on("signed_in", signed_in)7980################################################81# Automatically log in82################################################83remember_me = webapp_client.remember_me_key()84if misc.get_local_storage(remember_me)85redux.getActions('account').setState(remember_me: true)86# just in case, always show manual login screen after 45s.87setTimeout (->88redux.getActions('account').setState(remember_me: false)89), 4500090webapp_client.on "remember_me_failed", () ->91redux.getActions('account').setState(remember_me: false)92if redux.getStore('account')?.is_logged_in() # if we thought user was logged in, but the cookie was invalid, force them to sign in again93f = ->94if not misc.get_local_storage(remember_me)95alert_message(type:'info', message:'You might have to sign in again.', timeout:1000000)96setTimeout(f, 15000) # give it time to possibly resolve itself. SMELL: confused about what is going on here...9798# check if user has a has_remember_me cookie (regardless if it is valid or not)99# the "remember_me" is set to be http-only and hence not accessible from javascript (security)100{get_cookie, APP_BASE_URL} = require('./misc_page')101# for the initial month after the rebranding, we always set this to true to emphasize the sign in bar at the top102# TODO the following is disabled -- https://github.com/sagemathinc/cocalc/issues/2051103if false # misc.server_weeks_ago(4) > new Date("2017-05-20")104redux.getActions('account').setState(has_remember_me : get_cookie("#{APP_BASE_URL}has_remember_me") == 'true')105else106redux.getActions('account').setState(has_remember_me : true)107108# Return a default filename with the given ext (or not extension if ext not given)109# FUTURE: make this configurable with different schemas.110exports.default_filename = (ext, is_folder) ->111return default_filename_iso(ext)112#return default_filename_mac(ext)113114default_filename_iso = (ext, is_folder) ->115base = misc.to_iso(new Date()).replace('T','-').replace(/:/g,'')116if ext117base += '.' + ext118return base119120# This isn't used yet -- will not a config option in account settings.121default_filename_mac = (ext, is_folder) ->122switch ext123when 'zip'124return 'Archive.zip'125else126return 'untitled ' + (if is_folder then 'folder' else 'file')127128129