Merge branch 'master' of git.clinicalfuture.com:arvados
[arvados.git] / services / api / app / controllers / arvados / v1 / users_controller.rb
1 class Arvados::V1::UsersController < ApplicationController
2   def current
3     @object = current_user
4     show
5   end
6   def system
7     @object = system_user
8     show
9   end
10
11   class ChannelStreamer
12     Q_UPDATE_INTERVAL = 12
13     def initialize(opts={})
14       @opts = opts
15     end
16     def each
17       return unless @opts[:channel]
18       @redis = Redis.new(:timeout => 0)
19       @redis.subscribe(@opts[:channel]) do |event|
20         event.message do |channel, msg|
21           yield msg + "\n"
22         end
23       end
24     end
25   end
26       
27   def event_stream
28     channel = current_user.andand.uuid
29     if current_user.andand.is_admin
30       channel = params[:uuid] || channel
31     end
32     if client_accepts_plain_text_stream
33       self.response.headers['Last-Modified'] = Time.now.ctime.to_s
34       self.response_body = ChannelStreamer.new(channel: channel)
35     else
36       render json: {
37         href: url_for(uuid: channel),
38         comment: ('To retrieve the event stream as plain text, ' +
39                   'use a request header like "Accept: text/plain"')
40       }
41     end
42   end
43
44   def activate
45     if current_user.andand.is_admin && params[:uuid]
46       @object = User.find params[:uuid]
47     else
48       @object = current_user
49     end
50     if not @object.is_active
51       if not (current_user.is_admin or @object.is_invited)
52         logger.warn "User #{@object.uuid} called users.activate " +
53           "but is not invited"
54         raise ArgumentError.new "Cannot activate without being invited."
55       end
56       act_as_system_user do
57         required_uuids = Link.where(owner_uuid: system_user_uuid,
58                                     link_class: 'signature',
59                                     name: 'require',
60                                     tail_uuid: system_user_uuid,
61                                     head_kind: 'arvados#collection').
62           collect(&:head_uuid)
63         signed_uuids = Link.where(owner_uuid: system_user_uuid,
64                                   link_class: 'signature',
65                                   name: 'click',
66                                   tail_kind: 'arvados#user',
67                                   tail_uuid: @object.uuid,
68                                   head_kind: 'arvados#collection',
69                                   head_uuid: required_uuids).
70           collect(&:head_uuid)
71         todo_uuids = required_uuids - signed_uuids
72         if todo_uuids == []
73           @object.update_attributes is_active: true
74           logger.info "User #{@object.uuid} activated"
75         else
76           logger.warn "User #{@object.uuid} called users.activate " +
77             "before signing agreements #{todo_uuids.inspect}"
78           raise ArgumentError.new \
79           "Cannot activate without user agreements #{todo_uuids.inspect}."
80         end
81       end
82     end
83     show
84   end
85 end