cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
user.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.clm.models.user
22 # @author Maciej Nabożny <di.dijo@gmail.com>
23 #
24 
25 from django.db import models
26 from clm.models.cluster import Cluster
27 from clm.utils.exception import CLMException
28 
29 
30 ##
31 #
32 # @model{USER}
33 #
34 # CLM User may have access to one or many CMs.
35 # There's one default CM for each User. Quotas are set per CM, however
36 # general User data is defined globally in CLM, within this model.
37 #
38 class User(models.Model):
39  ## User's first name @field
40  first = models.CharField(max_length=63)
41  ## User's last name @field
42  last = models.CharField(max_length=63)
43  ## default Cluster. @field
44  default_cluster = models.ForeignKey(Cluster, null=True, blank=True, on_delete=models.SET_NULL)
45 
46  ## Users's login for authentication (unique) @field
47  login = models.CharField(max_length=63, unique=True)
48  ## User's password for authentication @field
49  password = models.CharField(max_length=255)
50  ## User's email (unique) @field
51  email = models.CharField(max_length=255, unique=True)
52  ## activation key (required only for specific registration types) @field
53  act_key = models.CharField(max_length=63, null=True, blank=True)
54  ## User's company or organization @field
55  organization = models.CharField(max_length=63)
56 
57  ## whether User is active and have rights to use the system @field
58  is_active = models.IntegerField()
59  ## whether User has admin priviledges @field
60  is_superuser = models.IntegerField(null=True, blank=True)
61  ## whether User has admin priviledges on any CM @field
62  is_superuser_cm = models.IntegerField(null=True, blank=True)
63 
64  ## @field
65  activation_date = models.DateTimeField(null=True, blank=True)
66  ## @field
67  last_login_date = models.DateTimeField(null=True, blank=True)
68 
69  class Meta:
70  app_label = 'clm'
71 
72  ##
73  #
74  # @returns{string} User first and last name
75  #
76  def __unicode__(self):
77  return self.first + ' ' + self.last
78 
79  @property
80  ##
81  #
82  # @returns{dict} this User's data
83  # \n fields:
84  # @dictkey{user_id,int} id of this User
85  # @dictkey{first,string} first name
86  # @dictkey{last,string} last name
87  # @dictkey{default_cluster_id,int} id of this User's default cluster
88  # @dictkey{login,string} login
89  # @dictkey{email,string} email
90  # @dictkey{act_key,string} activation key's content
91  # @dictkey{organization,string} organization
92  # @dictkey{is_active,bool} true for active User
93  # @dictkey{is_superuser,bool} true for User with admin privilidges
94  # @dictkey{activation_date,datetime.datetime} activation's date
95  # @dictkey{last_login_date,datetime.datetime} last login's date
96  #
97  def dict(self):
98  d = {}
99  d['user_id'] = self.id
100  d['first'] = self.first
101  d['last'] = self.last
102  d['default_cluster_id'] = self.default_cluster.id if self.default_cluster else 0
103  d['login'] = self.login
104  d['email'] = self.email
105  d['act_key'] = self.act_key or ''
106  d['organization'] = self.organization or ''
107  d['is_active'] = self.is_active or 0
108  d['is_superuser'] = self.is_superuser or 0
109  d['is_superuser_cm'] = self.is_superuser_cm or 0
110  d['activation_date'] = self.activation_date or ''
111  d['last_login_date'] = self.last_login_date or ''
112  return d
113 
114  @property
115  ##
116  #
117  # @returns{dict} very short version of User's data
118  # \n fields:
119  # @dictkey{user_id,int} id of this User
120  # @dictkey{first,string} first name
121  # @dictkey{last,string} last name
122  #
123  def short_dict(self):
124  d = {}
125  d['user_id'] = self.id
126  d['first'] = self.first
127  d['last'] = self.last
128 
129  return d
130 
131  @property
132  ##
133  #
134  # @returns{Group queryset} all instances of the groups this User is leader of
135  #
136  def own_groups(self):
137  return self.group_leader_set.all()
138 
139  @staticmethod
140  ##
141  #
142  # @parameter{user_id,int} primary index of the User
143  #
144  # @returns{User} instance of requested User
145  #
146  # @raises{user_get,CLMException}
147  #
148  def get(user_id):
149  try:
150  u = User.objects.get(pk=user_id)
151  except:
152  raise CLMException('user_get')
153  return u
154 
155  @staticmethod
156  ##
157  #
158  # @parameter{user_id,int} User's id
159  # @parameter{group_id,int} Group's id
160  #
161  # @returns{bool} True if User is the Group's leader
162  #
163  # @raises{user_permission,CLMException} User isn't the leader of the given Group
164  #
165  def is_leader(user_id, group_id):
166 
167  # No good, because i should import Group and there would be cross import
168  #
169  # g = Group.objects.get(pk=group_id)
170  # if g.leader_id == user_id:
171  # return True
172  # else:
173  # raise CLMException('user_permission')
174 
175  user = User.get(user_id)
176 
177  # user.group_leader_set.all() returns all the groups where the user is leader
178  if user.group_leader_set.filter(id__exact=group_id).exists():
179  return True
180  else:
181  raise CLMException('user_permission')
182 
183  @staticmethod
184  ##
185  #
186  # @parameter{user_id,int} User's id
187  #
188  # @returns{bool}
189  # @avail{True} - User is superuser
190  #
191  # @raises{user_permission,CLMException} User isn't superuser
192  #
193  def superuser(user_id):
194  user = User.get(user_id)
195  if not user.is_superuser:
196  raise CLMException('user_permission')
197  return True
198