Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This file is subject to the terms and conditions defined in
- file 'LICENSE', which is part of this source code package.
- © OtteIT s.r.o.
- All Rights Reserved.
- Author: Vilem Otte <[email protected]>
- */
- import { ErrorFactory } from "../util/error";
- import { Progress } from "../util/progress";
- import { System } from "../util/system";
- import { Url } from "../util/url";
- /**
- * User's profile class
- *
- * This class takes care of requests for user
- */
- export class UserProfile
- {
- /**
- * User's ID
- */
- public id: number;
- /**
- * User's login name
- */
- public login_name: string;
- /**
- * User's email
- */
- public email: string;
- /**
- * User's password (never read, only for update/creation)
- */
- public password: string;
- /**
- * Default constructor
- * @param id User's ID
- * @param login_name Login name
- * @param email Email address
- * @param password Password
- */
- constructor(id: number, login_name: string = "", email: string = "", password: string = "")
- {
- this.id = id;
- this.login_name = login_name;
- this.email = email;
- this.password = password;
- }
- /**
- * Create user record
- * @param progress Instance of Progress class where request progress is written
- * @returns Promise object with result of creation (or error data on failure)
- */
- public Send(progress: Progress)
- {
- var json = { "login_name": this.login_name, "email": this.email, "password": this.password };
- return Url.FetchJson(System.BaseURL() + "api/v1/user/create.php", true, json, progress);
- }
- /**
- * Deleta user record
- * @param progress Instance of Progress class where request progress is written
- * @returns Promise object with result of deletion
- */
- public Delete(progress: Progress)
- {
- var json = { "id": this.id };
- return Url.FetchJson(System.BaseURL() + "api/v1/user/delete.php", true, json, progress);
- }
- /**
- * Update user record
- * @param progress Instance of Progress class where request progress is written
- * @returns Promise object with result of update
- */
- public Update(progress: Progress)
- {
- var json = { "id": this.id, "login_name": this.login_name, "email": this.email, "password": this.password };
- return Url.FetchJson(System.BaseURL() + "api/v1/user/update.php", true, json, progress);
- }
- /**
- * Get user/users record/records
- * @param progress Instance of Progress class where request progress is written
- * @returns Promise object with array of user's records (or error data on failure)
- */
- public Request(progress: Progress)
- {
- var json = {};
- if (this.id > 0)
- {
- json = { "id": this.id };
- }
- return Url.FetchJson(System.BaseURL() + "api/v1/user/read.php", false, json, progress);
- }
- /**
- * Update user's avatar to a new file
- * @param user_id User ID for which avatar is modified
- * @param file New file to update
- * @returns Promise object with update result
- */
- public async UpdateAvatar(file: File, progress: Progress)
- {
- var media_id = 0;
- var json_user = { "id": this.id };
- var res_json = {};
- await Url.FetchJson(System.BaseURL() + "api/v1/user/read.php", false, json_user, progress).then(function(res)
- {
- if (res.result == 200 && res.user.length > 0)
- {
- media_id = res.user[0].avatar;
- }
- else
- {
- ErrorFactory.Show(res.status, res.statusText);
- }
- });
- if (media_id > 0)
- {
- var json_delete = { "id": media_id };
- await Url.FetchJson(System.BaseURL() + "api/v1/media/delete.php", true, json_delete, progress).then(function(res)
- {
- if (res.result != 200)
- {
- ErrorFactory.Show(res.status, res.statusText);
- }
- });
- }
- await Url.FetchSendFile(System.BaseURL() + "api/v1/media/upload.php", true, file, progress).then(function(res)
- {
- if (res.result == 200)
- {
- media_id = res.media;
- }
- else
- {
- ErrorFactory.Show(res.status, res.statusText);
- }
- });
- var json = { "id": this.id, "avatar": media_id };
- return Url.FetchJson(System.BaseURL() + "api/v1/user/update.php", true, json, progress);
- }
- /**
- * Get list of permissions for user
- * @param user_id User ID for which permission list is requested
- * @returns Promise object with list of permissions for user (or error data on failure)
- */
- public Permission(progress: Progress)
- {
- var json = { "user_id": this.id };
- return Url.FetchJson(System.BaseURL() + "api/v1/permission/read.php", true, json, progress);
- }
- /**
- * Update permission record
- * @param progress Instance of Progress class where request progress is written
- * @param permission_id Permission record ID to modify
- * @param permission_role New permission role
- * @param permission_level New permission level
- * @returns Promise object with result of permission update
- */
- public UpdatePermission(progress: Progress, permission_id: number, permission_role: string, permission_level: number)
- {
- var json = { "id": permission_id, "role": permission_role, "level": permission_level };
- return Url.FetchJson(System.BaseURL() + "api/v1/permission/update.php", true, json, progress);
- }
- /**
- * Add permission record for user
- * @param progress Instance of Progress class where request progress is written
- * @param permission_role Permission role to add
- * @param permission_level Permission level to set
- * @returns Promise object with result of permission add
- */
- public AddPermission(progress: Progress, permission_role: string, permission_level: number)
- {
- var json = { "user_id": this.id, "role": permission_role, "level": permission_level };
- return Url.FetchJson(System.BaseURL() + "api/v1/permission/create.php", true, json, progress);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement