-
Notifications
You must be signed in to change notification settings - Fork 2
/
User.js
110 lines (99 loc) · 2.96 KB
/
User.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* App User объект пользователя, должен узнавать обо всех изменениях от комета
*/
define("app/User", ["app/Logger", "app/Hub"], function (Logger, Hub) {
"use strict";
var global = (function () { //get global object
return this || eval.call(null, 'this');
} ()),
$ = global.$,
canCache,
can;
app.User = {
hasAvatar: false,
avatarReal: "",
title: null,
userId: null,
avatar25: "",
avatar50: "",
name:'',
money: 0,
statuses: {
profile: 0,
email: 0,
user: -1,
premium: 0
},
init: function () {
app.User.update(global.zarium);
Hub.sub("User.update", app.User.update );
Hub.sub("User.reload", app.User.reload );
},
reload:function(){
$.ajax({
url:"/user/getInfo",
dataType: "json",
cache:false,
success:app.User._reload
});
},
_reload:function(data, textStatus, jqXHR){
Logger.warn("User",data);
app.User.update(data.data.success);
},
update: function (data) {
$.each(data, function (key, value) {
app.User[key] = value;
});
Hub.pub("User.updated");
/* При обнвлении данных о пользователе, нужно обновлять обращение в шапке */
if(app.User.title) {
$('#loginBlock .log-name').text(app.User.title);
}
},
/**
* @description Check user permissions on some actions
*/
can: function (action) {
return $.isFunction(canCache[action]) ? canCache[action](this) : canCache[action];
}
}
app.User.init();
can = app.User.can;
/**
* Initialize cahce object
*/
canCache = can.cache = {};
/**
* @param {String|Array} action
* @param {String|Function} test
* @param {Boolean} force Rewrite test on action if test exists
*/
can.add = function (action, test, force) {
if (typeof canCache[action] === 'undefined' || force) {
if (!$.isArray(action)) {
canCache[action] = test;
return;
}
$.each(action, function (key, value) {
canCache[value] = test;
});
}
};
/**
* Some predefined permissions
*/
can.add(['searchInUse'], function (user) {
return user.sex;
});
can.add(['uploadPhoto'], function (user) {
return user.sex;
});
can.add(['writeMessage', 'addToFriends'], function (user) {
if(user.statuses.profile != undefined && user.statuses.profile == 1) {
return user.statuses.profile;
}
return false;
});
return app.User;
});